Android include标签的使用
来源:互联网 发布:qt tcp socket编程 编辑:程序博客网 时间:2024/05/01 19:42
在开发布局的时候会碰见一些布局重复利用的情况。比如两个不相干的的类,但是他们的布局有部分是一样的,那时候可以使用include标签来重复利用相同的layout。
来优化我们的xml文档,提高可读性。也能节省代码冗余。
下面我使用一个简单的例子来使用<include> 标签。
下面是需要重复使用的layout 。
demo_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="这个是测试效果" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/button1" android:layout_alignBottom="@+id/button1" android:layout_alignLeft="@+id/textView1" android:layout_marginLeft="63dp" android:text="Activity2" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginRight="62dp" android:layout_marginTop="15dp" android:text="Activity1" /></RelativeLayout>
Activity的布局
activity1.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Activity1" android:gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" /> <include layout="@layout/demo_layout"/></LinearLayout>
activity2.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Activity2" android:gravity="center" android:textAppearance="?android:attr/textAppearanceMedium" /> <include layout="@layout/demo_layout"/></LinearLayout>
在Activity中的使用<include>标签中layout的方法。
setContentView(R.layout.activity1_layout);Button button1 = (Button) findViewById(R.id.button1);Button button2 = (Button) findViewById(R.id.button2);button1.setEnabled(false);button2.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//点击事件自定义}});TextView text = (TextView) findViewById(R.id.textView1);text.setText("这个是Activity1");}
setContentView(R.layout.activity2_layout);Button button1 = (Button) findViewById(R.id.button1);Button button2 = (Button) findViewById(R.id.button2);button2.setEnabled(false);button1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {//点击事件自定义}});TextView text = (TextView) findViewById(R.id.textView1);text.setText("这个是Activity2");
可以直接通过ID来引用控件。
<include> 主要是用来精简了xml布局文档中的代码。 简单的效果图如下 :
- Android include标签的使用
- Android include标签的使用
- android include 标签的使用
- android include标签使用
- android中include标签的使用
- android中include标签的使用
- android include标签的简单使用
- android中include标签的使用
- Android中include标签的使用
- android中include标签的使用
- Android开发--include标签的使用
- android中include标签的使用
- android xml中include标签的使用
- android中include标签的使用
- android中include标签的使用
- Android中include标签的使用
- android中include标签的使用
- android中include标签的使用
- Todo List
- 用WORD2007 制作三线表
- Photon官方Lite类中Lite Concepts
- 数据库对象的缓存策略
- 第三周项目四
- Android include标签的使用
- 64位IIS中使用Microsoft.Jet.OLEDB.4.0操作EXCEL及ACCESS
- 网站页面内容倾斜技巧
- java 日期的小整理
- 美利达的山地车报价
- js中innerHTML,innerText,outerHTML的用法与差别
- Problem G
- Android Audio 02 - AudioTrack/AudioFlinger/AudioPolicyService 构架
- Android 获得imei 和 mac ,平板有时无法获取imei