代码之家  ›  专栏  ›  技术社区  ›  Ragunath Jawahar cephus

如何使用RelativeLayout获得以下结果?

  •  7
  • Ragunath Jawahar cephus  · 技术社区  · 14 年前

    图片: http://img838.imageshack.us/img838/1402/picse.png

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
    <TextView
        android:id="@+id/timer_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dip"
        android:textSize="30sp"
        android:layout_alignParentTop="true"
        android:text="@string/timer_start" />
    
    <ListView 
        android:id="@+id/questions_listview"
        android:layout_below="@id/timer_textview"  
        android:layout_width="wrap_content"
        android:layout_height="fill_parent" />
    
    <Button android:id="@+id/true_btn"
        android:text="@string/true_option"
        android:layout_alignParentBottom="true"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <Button
        android:id="@+id/false_btn"
        android:text="@string/false_option"
        android:layout_toRightOf="@id/true_btn"
        android:layout_alignBaseline="@id/true_btn"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    </RelativeLayout>
    
    2 回复  |  直到 14 年前
        1
  •  18
  •   plugmind    14 年前

    你可以用 LinearLayout RelativeLayout 只有这样布局:

    <RelativeLayout android:layout_width="fill_parent" ... >
    
    ...    
    
    <View android:id="@+id/helper" 
    android:layout_width="0dp"
    android:layout_height="0dp" 
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />
    
    <Button android:id="@+id/true_btn"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toLeftOf="@id/helper"/>
    
    <Button
    android:id="@+id/false_btn"
    android:layout_alignBaseline="@id/true_btn"
    android:layout_alignParentRight="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" 
    android:layout_toRightOf="@id/helper" />
    
    </RelativeLayout>
    

    你真的不能使用嵌套布局吗?

        2
  •  0
  •   DonGru    14 年前

    如果你把按钮改成

    <Button android:id="@+id/true_btn"
        android:text="@string/true_option"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    
    <Button
        android:id="@+id/false_btn"
        android:text="@string/false_option"
        android:layout_toRightOf="@id/true_btn"
        android:layout_alignBaseline="@id/true_btn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    

    尽管 false_btn

    增加第一个的宽度 true_btn minWidth 最小宽度 它的一半