使用嵌套布局定义特定的xml布局

时间:2013-02-14 09:48:19

标签: android xml xml-layout

我正在设计我的第一个Android应用程序,我正试图摆脱创建XML布局的困境。 (我对Java有很多经验)

基本上我想做的事情:

http://i.imgur.com/ZgbvYsX.png

大纲描述的地方:

蓝色:基本视图(我可以在其中设置文字)

红色:一个ButtonView(用户可以同步数据)

绿色:嵌套的RelativeLayout(我要添加很多其他东西)

更具体地说,我想做什么:

enter image description here

所以我的问题是:我如何设置不同的布局?我设置蓝色和红色视图时遇到了麻烦,因为我希望红色视图填满屏幕宽度的25%左右(蓝色填满最后75%)。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

使用layout_weight属性指定视图占用的屏幕空间百分比。

像这里一样,例如:

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

     <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent"
     android:orientation="horizontal">

           <EditText android:layout_height="wrap_content" android:layout_width="0dip"
           android:layout_weight="3"/>

           <Button android:layout_height="wrap_content" android:layout_width="0dip"
           android:layout_weight="1"/>

     </LinearLayout>

     //Other view

</LinearLayout>

答案 1 :(得分:0)

使用以下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="vertical" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_weight="0.75">

        <TextView
            android:id="@+id/headingTextView"
            style="@android:style/TextAppearance.WindowTitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:text="Cafe Vigeos"
            android:textColor="@android:color/white"
            android:textSize="30sp" />
         </RelativeLayout>
         <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:layout_weight="0.25">
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/nested_relative_layout"
        android:layout_width="match_parent"
        android:layout_height="fill_parent" >
    </RelativeLayout>

</LinearLayout>
相关问题