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

Android 布局嵌套

2013-09-04 20:46 155 查看
本来用RelativeLayout的布局方式就很灵活,但是还是有写需求达不到。只能通过布局嵌套的方式来实现。

首先activity_main采用的是RelativeLayout布局方式,然后通过include标签引入一个LinearLayout的布局文件。

建一个xml文件,取名twobtn.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"

android:orientation="horizontal"
>

<Button
android:layout_width="0sp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="@string/equal"/>

<Button
android:layout_width="0sp"

android:layout_weight="1"

android:layout_height="wrap_content"

android:text="@string/clear"/>

</LinearLayout>

注意以上代码中的android:layout_weight属性。
然后在主layout文件中加入以下代码

<include

android:id="@+id/btn"
layout="@layout/twobtn"

android:layout_below="@id/theTextView"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_marginLeft="0sp"

android:layout_marginRight="0sp"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: