Android <include/>元素的使用

这个代码的作用就是提高代码的重用性。它可以把已经定义好的一个布局当成另外一个布局的中的一个元素。

下面举一个例子:

textview_1.xml文件

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="第一个布局组件——TextView组件">
    
</TextView>

ratingbar.xml文件

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <RatingBar 
        android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
    
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/ratingbar"
        android:text="第二个布局组件——包含有两个"/>

</RelativeLayout>

主布局文件

<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"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <include layout="@layout/textview_1"/>
    <include layout="@layout/ratingbar"/>

</LinearLayout>

运行结果是:

posted @ 2014-11-25 15:22  RoperLee  阅读(169)  评论(0)    收藏  举报