在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include /> 标签来重用layout代码。
btn.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button">
</Button>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button">
</Button>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include android:id="@+id/in1" layout="@layout/btn"/>
<include android:id="@+id/in2" layout="@layout/btn"/>
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<include android:id="@+id/in1" layout="@layout/btn"/>
<include android:id="@+id/in2" layout="@layout/btn"/>
<TextView android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
</LinearLayout>
TestActivity:
package com.hilary;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hialry.R;
/**
*
*@author:hilary
*@Date:2011-12-8
*@description:
*
**/
public class TestActivity extends Activity {
private TextView tv = null;
private LinearLayout ll = null;
private LinearLayout ll2 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
//如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,
//如果就包含同一个xml文件侧不需要此步操作
ll = (LinearLayout) findViewById(R.id.in1);
ll2 = (LinearLayout) findViewById(R.id.in2);
ll.setBackgroundColor(Color.RED);
Button btn = (Button) ll.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("My name is hilary");
}
});
Button btn2 = (Button) ll2.findViewById(R.id.btn);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(" You select second Button!");
}
});
}
}
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hialry.R;
/**
*
*@author:hilary
*@Date:2011-12-8
*@description:
*
**/
public class TestActivity extends Activity {
private TextView tv = null;
private LinearLayout ll = null;
private LinearLayout ll2 = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.tv);
//如果一个布局文件中包含同一个xml文件,这两个xml中的控件Id是一样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,
//如果就包含同一个xml文件侧不需要此步操作
ll = (LinearLayout) findViewById(R.id.in1);
ll2 = (LinearLayout) findViewById(R.id.in2);
ll.setBackgroundColor(Color.RED);
Button btn = (Button) ll.findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText("My name is hilary");
}
});
Button btn2 = (Button) ll2.findViewById(R.id.btn);
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tv.setText(" You select second Button!");
}
});
}
}
- 如果没有include标签,所有布局代码都写在一个xml文件中,界面会显得很冗余,可读性很差。而且界面加载的时候是按照顺序加载的,前面的布局不能调用其后面的布局id。而采用include后,一个include中可以引用其后的include中的布局id属性 -->
相关推荐
关于xml布局中标签<include />的使用的一个Demo
尽管Android提供了各种组件来实现小而可复用的交互元素,你也可能因为布局需要复用一个大组件。为了高效复用完整布局,你可以使用<include>和标签嵌入另一个布局到当前布局。所以当你通过写一个自定义视图创建独立UI...
merge结合include优化android布局,效果不知道,个人感觉使用上也有很大的局限,不过还是了解一下,记录下来。 布局文件都要有根节点,但android中的布局嵌套过多会造成性能问题,于是在使用include嵌套的时候我们...
下面的XML布局显示一个图片,并且有一个标题位于其上方。这个结构相当的简单;FrameLayout里放置了一个ImageView,其上放置了一个TextView: <FrameLayout xmlns:android=http://schemas.android.com/apk/res/a
我们在android的布局文件中,常会遇到一些相同的布局,每个页面都写,一是比较麻烦,二是一旦有修改还得改多个文件.这个时候我们就可以用到include了. 非常简单的使用,下面看代码 include的文件scollandlisttitle...
app开发过程中,会遇到不同页面里有相同的布局,这时我们可以将这些通用的布局提取出来到一个单独的layout文件里,再使用<include>标签引入到相应的页面布局文件里,主要通过include的layout属性引用。 举个栗子...
本文实例讲述了Android开发中include控件用法。分享给大家供大家参考,具体如下: 我们知道,基于Android系统的应用程序的开发,界面设计是非常重要的,它关系着用户体验的好坏。一个好的界面设计,不是用一个xml...
在一个项目中我们可能会需要用到相同的布局设计,如果都写在一个xml文件中,代码显得很冗余,并且可读性也很差,所以我们可以把相同布局的代码单独写成一个模块,然后用到的时候可以通过<include> 标签来重用layout...
Android平台提供了大量的UI构件,你可以将这些小的视觉块(构件)搭建在一起,呈现给用户复杂且有用的画面。...在Android XML布局文件里,一般,每个标签都对应一个真实的类实例(这些类一般都是View的子类
In particular, note that some Linux distributions may include JDK 1.4 or Gnu Compiler for Java, both of which are not supported for Android development----------------------------------- Android 编程...
比较好的一种做法是使用include标签,复用同一个xml文件来实现布局的复用。但是这种方法是通过代码的方式来设置标题,左右按钮等其他的属性,会导致布局属性和Activity代码耦合性比较高。 因此,我们要通过自定义...
项目中经常会遇到几种页面:加载中、无网络、无数据、出错四种情况,传统的方式是通过include相关的布局,逐个分情况设置显示或隐藏,这样繁琐的过程一直是个痛点,于是参考了 Weavey 的封装套路,自己重新写了一套...
Android——Android lint工具项目资源清理 最近维护的项目已经有两年多,经过很多前辈的迭代,项目并没有变得健壮...尽量使用include、merge、ViewStub标签,尽量不存在冗余嵌套及过于复杂布局,尽量使用GONE替换INVISIBL
一、Layout XML文件常见的两种模块加载方式 1、静态加载:被加载的模块和其它模块加载的时间一样。 <include layout=@layout/otherLayout/> 2、动态加载:需要被加载的模块初始时并没有被加载进内存,在你...
可重用的XML布局(使用include标记) 使用第三方库( ) 具有Android Piccasso库的ImageView 共享首选项 网页浏览 使用Intent发送电子邮件 讨论单例设计模式 讨论Android编译系统。 关键字:Dalvik ButterKn
最近在定制Android系统音量条,发现代码还是蛮多的,下面总结一下。...在代码中可以发现volume_dialog.xml这个文件,这个文件就是承载音量条的布局了,在layout文件夹找到打开会发现这个布局很简单,只是include
首先要写三个xml布局文件,我这里的布局文件是使用了include标签嵌入的,代码如下 headbar_toolbar.xml <?xml version=1.0 encoding=utf-8?> <android.support.v7.widget.Toolbar xmlns:android=...
界面的思路是利用ViewPager+Fragment实现,所以activity_main.xml中添加一个ViewPager。顶部和底部include的顶部栏和底部栏后面再说。 MainActivity的界面activity_main.xml: <?xml version="1.0" encoding="utf-...
所以,一般情况下,我们需要把相同布局的代码单独写成一个模块,然后在用到的时候,可以通过<include> 标签来重用layout的代码。 常见的,有的应用在最上方会有一个标题栏。类似下图所示。 图 标题栏的示例 ...
线线布 局模式可以在比较简单的include进行完成,最重要的一点就是:我们可以自己通过重写 方法或者通过实现View或者Layout等类进行扩充项目需 要的布局(或者控件) ,在学习界面中,我发现Android为我们提供了很好的...