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
foot.xml
布局文件:
效果图:
通过android:layout_below=”@id/include_title” 和 android:layout_above=”@id/include_foot” 将图片填充中间位置,从而不需要通过android:layout_height来调节让foot安置在底部。
但是刚开始遇到一个问题,在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安置在底部。
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories