Android UI优化之include标签的使用
来源:互联网 发布:我爱你韩国知乎 编辑:程序博客网 时间:2024/06/07 23:10
在Android的官方文档中UI的优化主要涉及<include />、<merge />、<ViewStub />三类标签,本文主要讲解<include />的使用,其他两种见后文。
<merge />:http://blog.csdn.net/u012721519/article/details/51229291
<ViewStub />:http://blog.csdn.net/u012721519/article/details/51231469
在Android的开发工程中,一个良好的项目开发,不仅需要有良好的代码编写习惯,当然优化也必不可少,其中UI的优化也是其中的重要一部分,避免代码的冗余,是一个良好优秀的开发习惯和后期维护必不可少的内容之一。
<include />:
能够重用布局文件,把相同的代码写成一个单独的模块,在其他地方复用,减少代码的冗余。
注意:
可以单独使用layout属性,在<include />里定义ID属性,但在layout也定义了相关ID属性,则layout属性将会覆盖<include />属性。
也可在一个UI设计里面复用同一个<include />模块,但需注意设置LinerLayout相关方法,如下:
ll1 = (LinearLayout)findViewById(R.id.in1); tv1 = (TextView)ll1.findViewById(R.id.tv);
通过LinearLayout方法寻找id,否则会出现id复用的情况。
详细代码如下:
include.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:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF0000" android:text="aa"/></LinearLayout>
activity_main.xml
<LinearLayout 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" android:orientation="vertical"> <include android:id="@+id/in1" android:layout_width="100dp" android:layout_height="wrap_content" layout="@layout/include" /> <include android:id="@+id/in2" android:layout_width="fill_parent" android:layout_height="wrap_content" layout="@layout/include"/></LinearLayout>
复用了两次<include />,本人主要是用于测试使用。
MainActivity.java
package example.com.includetest;import android.graphics.Color;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private TextView tv1,tv2; private LinearLayout ll1 = null; private LinearLayout ll2 = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initUI(); initData(); } private void initData() { tv1.setText("include 事件一"); tv2.setText("include 事件二"); tv2.setTextColor(Color.BLUE); } private void initUI() { ll2 = (LinearLayout)findViewById(R.id.in2); ll1 = (LinearLayout)findViewById(R.id.in1); tv1 = (TextView)ll1.findViewById(R.id.tv); tv2 = (TextView)ll2.findViewById(R.id.tv); }}
initData方法主要是用于测试,证明两个复用的<include />均可使用
运行效果如下:
源码下载地址:http://download.csdn.net/detail/u012721519/9500597
Good luck!
Write by Jimmy.li
- Android UI优化之include标签的使用
- android UI 优化之<include>标签
- android布局优化之<include>标签的使用
- Android UI 优化 使用<include/>和 <merge />标签
- Android UI 优化 使用<include/>和 <merge />标签
- Android UI 优化 使用<include/>和 <merge />标签
- Android UI 优化 使用<include/>和 <merge />标签
- Android UI 优化 使用<include/>和 <merge />标签
- Android UI优化之merge标签的使用
- Android UI优化之ViewStub标签的使用
- Android layout之优化:使用include和merge 标签
- Android layout之优化:使用include和merge 标签
- android UI进阶之布局的优化---使用include和merge
- android UI进阶之布局的优化---使用include和merge
- android UI进阶之布局的优化---使用include和merge
- Android include标签的使用
- Android include标签的使用
- android include 标签的使用
- 找最长等差数列的长度
- Leetcode #53. Maximum Subarray 最大区间和 解题报告
- Edison编译时显示No such file or directory
- Repeat和AspNetPager控件
- Android Service
- Android UI优化之include标签的使用
- atexit()函数的使用
- Android多线程与线程池
- HDU 1219 AC Me(字母统计)
- ZOJ Problem Set - 3938 (模拟)
- SQL
- Linux文件与文件系统的压缩与打包
- C语言中.h和.c文件解析(很精彩)
- OC可变数组(NSMutableArray)相关方法