Android Api Demos登顶之路(六十三)Content-->Resources Smallest Width

来源:互联网 发布:软件招聘平台 编辑:程序博客网 时间:2024/06/05 03:01

这个demo演示 的主要是布局文件的技巧。
本例的基本思路是:
1.创建一个布局文件activity_row
该布局中只是定义了两个文本框,通过layout_weight属性,设置两个TextView平均分配屏幕的宽度。
需要注意的是这里使用了Merge节点。该节点可以使被include引用的布局在被嵌入父级结构中后可以很好的将它所包含的子集融合到父级结构中,而不会出现冗余的节点。

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="horizontal">        <TextView             android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="Default width #1"            android:layout_marginLeft="4dp"            android:layout_marginRight="4dp"            android:background="#800000ff"/>        <TextView             android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="Default width #2"            android:layout_marginLeft="4dp"            android:layout_marginRight="4dp"            android:background="#800000ff"/>    </LinearLayout></merge>

2.activity_coluns.xml。在该布局中定义了两个均分屏幕剩余高度的FrameLayut。
并在该FrameLayout使用include引入了上面设置的布局

<?xml version="1.0" encoding="utf-8"?><merge xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout         android:layout_width="match_parent"        android:layout_height="match_parent"        android:orientation="vertical">        <FrameLayout             android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:padding="4dp"            android:background="#8000ff00">            <include layout="@layout/activity_row"/>        </FrameLayout>        <FrameLayout             android:layout_width="match_parent"            android:layout_height="0dp"            android:layout_weight="1"            android:padding="4dp"            android:background="#8000ff00">            <include layout="@layout/activity_row"/>        </FrameLayout>    </LinearLayout></merge>

3.定义主布局,activity_main,只是定义了一个TextView尔后将剩余的界面都归属了一个Framelayout.并在FrameLayout当中引入了activity_coluns.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"    android:orientation="vertical"     android:padding="4dp">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_weight="0"        android:textAppearance="?android:attr/textAppearanceMedium"        android:text="@string/info" />    <FrameLayout         android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"        android:background="@android:drawable/gallery_thumb">        <include layout="@layout/activity_colums"/>    </FrameLayout></LinearLayout>
0 0