如何使用RelativeLayout的我实现以下目的?目的、如何使用、RelativeLayout

2023-09-06 00:24:42 作者:綄镁の爱

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

如何使PIC中的布局。 2使用RelativeLayout的而已。下面是第一张截图的XML布局。

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
        的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT>
<的TextView
    机器人:ID =@ + ID / timer_textview
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=中心
    机器人:layout_marginTop =10dip
    机器人:TEXTSIZE =30sp
    机器人:layout_alignParentTop =真
    机器人:文本=@字符串/ timer_start/>

<的ListView
    机器人:ID =@ + ID / questions_listview
    机器人:layout_below =@ ID / timer_textview
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =FILL_PARENT/>

<按钮机器人:ID =@ + ID / true_btn
    机器人:文本=@字符串/ true_option
    机器人:layout_alignParentBottom =真
    机器人:layout_weight =1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>

<按钮
    机器人:ID =@ + ID / false_btn
    机器人:文本=@字符串/ false_option
    机器人:layout_toRightOf =@ ID / true_btn
    机器人:layout_alignBaseline =@ ID / true_btn
    机器人:layout_weight =1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT/>
< / RelativeLayout的>
 

解决方案

您可以使用的LinearLayout 底部实现这一目标。但是,要使用 RelativeLayout的布局只有这样:

 < RelativeLayout的机器人:layout_width =FILL_PARENT...>

...

<查看机器人:ID =@ + ID /帮手
机器人:layout_width =0dp
机器人:layout_height =0dp
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizo​​ntal =真/>

<按钮机器人:ID =@ + ID / true_btn
机器人:layout_alignParentBottom =真
机器人:layout_alignParentLeft =真
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toLeftOf =@ ID /帮手/>

<按钮
机器人:ID =@ + ID / false_btn
机器人:layout_alignBaseline =@ ID / true_btn
机器人:layout_alignParentRight =真
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toRightOf =@ ID /帮手/>

< / RelativeLayout的>
 
RelativeLayout 相对布局

你不能真正使用嵌套布局?

Pictures: http://img838.imageshack.us/img838/1402/picse.png

How do I make the layout in Pic. 2 using RelativeLayout ONLY. Below is the xml layout of the first screenshot.

<?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>

解决方案

You could use LinearLayout at bottom to achieve this. But you want to use RelativeLayout layout only so:

<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>

Can't you really use nested layouts?