使用include实现布局(layout)复用
来源:互联网 发布:剑三淘宝cos店铺 编辑:程序博客网 时间:2024/05/05 03:11
假使我们遇到这么一种情况,我们需要开发一个app,这个app的基本所有的Activity都使用到了相同的布局,我们该如何设计?我们是给这些个Activity布局文件都统一加上一样的布局代码吗?但是这样维护起来很麻烦,修改很不方便,Android布局中有没有一种类似于编程语言的include语法呢?答案是有的,但是sdk的demo并没有说出使用方法,但这并不说明不好使用,其实很简单。下面的IncludeXmlTest工程给出了样式。
我们新建一个IncludeXmlTest工程,我们先从布局文件上下手,我们新建一个真正的real.xml文件,来实现我们的布局,代码如下:
XML/HTML代码
- <?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" >
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="show" />
- <TextView
- android:id="@+id/text"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="" />
- </LinearLayout>
然后在我们需要引入的xml文件中,include这个real.xml就可以了,我这个main.xml代码如下:
XML/HTML代码
- <?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/include"
- layout="@layout/real"/>
- </LinearLayout>
这个include部分就是引入了real.xml的布局,之后我们在程序中只需要布局main.xml文件即可:
Java代码
- public class IncludeXmlTestActivity extends Activity {
- private TextView mTextView;
- private Button mButton;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- /**
- * 设置view
- * */
- public void initView(){
- mTextView = (TextView) findViewById(R.id.text);
- mButton = (Button)findViewById(R.id.button);
- mButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- mTextView.setText("hello, i am here");
- }
- });
- }
- }
怎么样简单吧,大家在重复布局的时候一定要记住使用哦,最后看下运行效果:
运行程序
点击按钮,显示textView的文字
- 使用include实现布局(layout)复用
- 使用include实现布局(layout)复用
- Android Layout中使用include标签实现UI布局重用
- Android Layout中使用include标签实现UI布局重用
- 使用Auto Layout-代码实现自动布局
- 【Android Training - Performance】提高显示布局文件的性能[Lesson 2 - 使用include标签重用Layout]
- 提高显示布局文件的性能 2 - 使用include标签重用Layout
- Android Training - 提升布局文件的性能(Lesson 2 - 使用include标签重用Layout)
- xml中使用include引入布局,layout属性设置不管用
- 在布局中使用include引入其他layout时引发click事件不触发的问题
- Android layout 使用include和merge 标签
- Android在layout xml中使用include
- 【layout】Android中include的使用
- Android在layout xml中使用include
- Android的Layout中 使用include
- Android在layout xml中使用include .
- Android在layout xml中使用include
- Android在layout xml中使用include
- android缓存策略
- JDK Tools and Utilities
- 为什么Android应用开发总要比iOS晚?
- 批量还原数据库
- rtmp协议简单解析以及用其发送h264的flv文件
- 使用include实现布局(layout)复用
- XCode4.2-使用复杂的触摸和手势UIXXGestureRecognizer
- Bellman-Ford算法
- 用HttpClient来模拟浏览器GET,POST
- Mysql常用命令行大全
- windows 下安装Oracle EBS R12 问题记录
- 中华英才网裁员,五成员工可拿(N+3)*月薪的离职补偿
- Hadoop v2(Yarn)集群配置(ubuntu 12.04)
- 排序专题(四) / 不稳定的内部排序 / 堆排序