2023年7月7日发(作者:)
androidinclude控件详解在Android的开发中,我们知道布局⽂件可以让我们很⽅便的对各个UI控件进⾏位置安排跟属性设置,⽽在程序中可以直接取得控件并赋予对应操作功能。但是,如果是⼀个复杂的界⾯设计,我们把所有布局都放在⼀个⽂件中来描述,那这个⽂件会显得⽐较臃肿⽽结构则变得⽆法清晰了。为此,Android为我们提供了⼀个武功⾼强的⾼⼿,这个⾼⼿的特异功能就是能够将⼏个不同的布局⽂件整合在⼀起,它的名字叫include,听名字就知道是包含的意思,当然是包括多个布局。由于是讲布局的安排跟组合,那我们这⾥就只拿布局⽂件来解析下,其他程序代码跟其他程序没区别。第⼀个例⼦:这⾥我们以最简单的控件TextView来举例,总共假设3个布局⽂件,其中⼀个布局包含了其他两个⼦布局。⽗布局layoutP:
android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> android:layout_width="fill_parent" layout="@layout/includeB" />
⼦布局⼀layoutA:
android:text="随时随地,即兴时代!" android:layout_width="wrap_content" android:layout_height="wrap_content">
⼦布局⼆layoutB:
android:text="" android:layout_width="wrap_content" android:layout_height="wrap_content">
通过以上layoutP中的整合,layoutA与layoutB就成为layoutP中的⼦元素,不仅使得整个布局代码结构清晰,提⾼了可读性,⽽且可以将界⾯排版中的功能模块清楚的划分。第⼆个例⼦:如果在程序中多次⽤到⼀部分相同的布局,可以先将这部分布局定义为⼀个单独的XML,然后在需要的地⽅通过
android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" >
[/align] xmlns:android="/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" />
TestActivity:
package ; import ty; import ; import ; import ; import kListener; import ; import ; import Layout; import ew;[/size] import .R; 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) {
te(savedInstanceState);
setContentView();
tv = (TextView) findViewById();
//如果⼀个布局⽂件中包含同⼀个xml⽂件,这两个xml中的控件Id是⼀样的,当需要操作这些控件时,需要通过定义这两个View来加以区分,
//如果就包含同⼀个xml⽂件侧不需要此步操作
ll = (LinearLayout) findViewById(1);
ll2 = (LinearLayout) findViewById(2);
kgroundColor();
Button btn = (Button) ewById();
lickListener(new OnClickListener() {
@Override
public void onClick(View v) {
t("My name is hilary");
}
});
Button btn2 = (Button) ewById();
lickListener(new OnClickListener() {
@Override
public void onClick(View v) {
t(" You select second Button!");
}
});
}
}
这只是在xml⽂件中引⼊另⼀种布局的⼀种⽅法,我们还可以在代码中直接引⼊,⽽不需要在xml中定义要引⼊的⽂件.
发布者:admin,转转请注明出处:http://www.yc00.com/xiaochengxu/1688676748a161743.html
评论列表(0条)