您的位置:首页 > 移动开发 > Android开发

Android--使用include调用布局

2015-08-12 12:18 537 查看
今天写界面时,为了提高效率,避免重复,将界面中的title和foot分别写成两个xml布局文件,只需在需要添加的xml中填写2句include语句即可。

但是刚开始遇到一个问题,在xml文件头添加 并没有问题,

但是在尾部添加发现 foot并没有一直在底部,而是紧随着上面的内容。

为了让foot置底,只能通过调节中间控件的android:layout_height属性。

解决办法:

将原LinearLayout改成RelativeLayout后,再分别在title和foot文件中添加 android:layout_alignParentTop=”true” 和 android:layout_alignParentBottom=”true” 来实现

一个简单的例子:

title.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="wrap_content"
android:orientation="vertical"
android:layout_alignParentTop="true"
android:background="@color/title_color">

<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="72dp"
android:gravity="center"

android:textSize="24sp"
android:textColor="@color/white"
/>
</LinearLayout>


foot.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="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:background="@color/foot_color">
<View
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@color/foot_line"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="210dp"
android:orientation="vertical" >

<TextView
android:id="@+id/foot_hint"
android:layout_width="match_parent"
android:layout_height="95dp"
android:paddingLeft="22dp"
android:paddingTop="22dp"

android:textColor="@color/foot_word_color"
android:textSize="23sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:layout_marginTop="20dp"
android:orientation="horizontal"
>

<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/btn_stop"
android:layout_width="135dp"
android:layout_height="46dp"
android:layout_marginLeft="48dp"
android:background="@drawable/bnt_selector"
android:gravity="center"
android:text="@string/stop"
android:textColor="@color/white"
android:textSize="21sp" />

<Button
style="?android:attr/borderlessButtonStyle"
android:id="@+id/btn_next"
android:layout_width="135dp"
android:layout_height="46dp"
android:gravity="center"
android:text="@string/next"
android:textColor="@color/white"
android:textSize="21sp"
android:background="@drawable/bnt_selector"
android:layout_marginLeft="72dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>


布局文件:

<?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="match_parent"
android:orientation="vertical"
android:background="@color/white">
<!-- 标题 -->
<include
android:id="@+id/include_title"
layout="@layout/title" >
</include>
<!-- 尾文件 -->
<include
android:id="@+id/include_foot"
layout="@layout/foot" >
</include>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/include_title"
android:layout_above="@id/include_foot"
>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/sound_image" />
</LinearLayout>

</RelativeLayout>


效果图:



通过android:layout_below=”@id/include_title” 和 android:layout_above=”@id/include_foot” 将图片填充中间位置,从而不需要通过android:layout_height来调节让foot安置在底部。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Android xml 布局 界面