关于xmlns:tools和android:onClick使用

From: http://blog.csdn.net/jwzhangjie/article/details/23918993

这篇之前理解有误,感谢2楼apples_benben的回复。

现在建立项目的时候在布局文件里面都会有一个 如下面的内容:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.             xmlns:tools="http://schemas.android.com/tools"  ---  
  3.             android:orientation="vertical"  
  4.             android:layout_width="fill_parent"  
  5.             android:layout_height="fill_parent"  
  6.             tools:context=".MainActivity"     ----  
  7.         />  

 

当然你不用也可以删除掉,tools:context="activity name"这一句不会被打包进APK。只是ADT的Layout Editor在你当前的Layout文件里面设置对应的渲染上下文,说明你当前的Layout所在的渲染上下文是activity name对应的那个activity,如果这个activity在manifest文件中设置了Theme,那么ADT的Layout Editor会根据这个Theme来渲染你当前的Layout。就是说如果你设置的MainActivity设置了一个Theme.Light(其他的也可以),那么你在可视化布局管理器里面看到的背景啊控件啊什么的就应该是Theme.Light的样子。仅用于给你看所见即所得的效果而已。(One more thing: The "tools" namespace is special. The android packaging tool knows to ignore it, so none of those attributes will be packaged into the APK. We're using it for extra metadata in the layout. It's also where for example the attributes to suppress lint warnings are stored -- as tools:ignore.)

 

android:onClick

很多时候我们写点击事件是在java代码里面,如果你动态不修改按钮的属性的时候,可以将点击事件写在xml里面,看下面代码:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical"  
  7.     tools:context=".Test" >  
  8.   
  9.   
  10.     <Button  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:onClick="clickChange"  
  14.         android:text="点击" />  
  15.   
  16.   
  17. </LinearLayout>  
  18. </span>  

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:14px;">public class Test extends Activity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.test);  
  7.     }  
  8.   
  9.     public void clickChange(View v) {  
  10.         Intent intent = new Intent(this, MainActivity.class);  
  11.         startActivity(intent);  
  12.         overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);  
  13.     }  
  14.   
  15. }</span>  

在Test.java中我没有获取test.xml布局中的Button实例,同样我也没有在test.xml里面设置“点击”按钮的id,但是我在按钮属性里面设置了android:onClick="clickChange"对应于Test.java中的public void clickChange(View v) 方法,从上面的例子中可以看出设置tools:context后,我们可以简化一些组件实例,就像"点击"
按钮只实现了一个点击功能,我们就没有必要再获取id然后实现他的onclick事件,简化了代码。
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值