文章目录
- 1、简介
- 2、include xml
1、简介
为了优化Android 界面加载
2、include xml
假设我们现在有一个 textView多个界面公用,如果每个界面都写一个 那么 就比较耗费时间,耗费资源,我们通过 include 的方式 来进行 公用资源的加载
- 代码结构
2)until_text.xml 文件
公用的 textView
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello Unit Test"
android:textSize="30dp"
android:textColor="@color/colorAccent">
</TextView>
3)activity_main.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.lum.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="40dp"
android:onClick="onClick"/>
<include
layout="@layout/until_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimary"
android:textSize="90dp"/>
</LinearLayout>
我们看到 include 里设置的 android:textColor , android:textSize 并没有生生效,而且还是textView 内部的
android:layout_width ,android:layout_height 生效了 是因为 until textView 里面也有。
android:layout_* 如果也想生效 在 公共的 xml里也要有
4)activity_second.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">
<include
layout="@layout/until_text"/>
</LinearLayout>