Android Relativelayout两个按钮右键一个按钮

时间:2013-04-18 11:46:40

标签: android relativelayout android-xml

我想知道如何使用Relativelayout来制作这样的结构:

三个按钮,我们称之为A B和C;我想把B和C一个放在另一个上面,并且它们都在A的右侧作为一个匹配相同宽度和高度A的单个块(所以我们将B和C的半高放在A但是相同的宽度)。

由于我无法在此处发布图片,因此您可以找到我正在寻找的图片:http://img191.imageshack.us/img191/9765/stackg.jpg

我写的关于这部分的代码就是这个:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/black"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <Spinner
        android:id="@+id/A"
        android:layout_width="0dp"
        android:layout_height="96dp"
        android:layout_below="@id/another_block_that_fills_all_the_line"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/B" />

    <Spinner
        android:id="@id/B"
        android:layout_width="96dp"
        android:layout_height="48dp"
        android:layout_below="@id/another_block_that_fills_all_the_line"
        android:layout_alignParentRight="true" />

    <Spinner
        android:id="@id/C"
        android:layout_width="96dp"
        android:layout_height="48dp"
        android:layout_below="@id/B"
        android:layout_alignParentRight="true" />

</RelativeLayout>

问题在于它不会随着每个屏幕缩放,因为我必须设置静态大小。我尝试了很多不同的解决方案,比如使用layout_align的所有可能组合,甚至随机地更改wrap_content,但是,如果不使用静态大小,我无法按照我想要的方式使用它们。

3 个答案:

答案 0 :(得分:1)

LinearLayoutlayout_weight一起使用。试试这个布局

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

    <Button android:layout_width="0dip" android:layout_height="fill_parent"
        android:layout_weight="1.0"/>

    <LinearLayout android:layout_width="0dip" android:layout_height="fill_parent"
        android:layout_weight="1.0" android:orientation="vertical">

        <Button android:layout_width="fill_parent" android:layout_height="0dip"
            android:layout_weight="1.0"/>

        <Button android:layout_width="fill_parent" android:layout_height="0dip"
            android:layout_weight="1.0"/>

     </LinearLayout>
</LinearLayout>

答案 1 :(得分:0)

试试这个,

<?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:background="@android:color/black">
    <Button
        android:id="@+id/A"
        android:layout_width="wrap_content"
        android:layout_centerInParent="true"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/relativeLayout"
        android:layout_alignBottom="@+id/relativeLayout"
        android:text="AAA"/>
    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/A"
        android:layout_centerVertical="true"
        android:background="@android:color/black">
        <Button
            android:id="@+id/B"
            android:layout_width="wrap_content"
            android:text="BBB"
            android:layout_height="wrap_content"/>
        <Button
            android:id="@+id/C"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/B"
            android:text="CCC"/>
     </RelativeLayout>
</RelativeLayout>

答案 2 :(得分:0)

应如下所示

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="10dp" >
<View
    android:id="@+id/helper"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<Spinner
    android:id="@+id/A"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@id/helper"
    android:background="@android:color/black" />
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/A"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/A"
    android:layout_marginLeft="10dp"
    android:layout_toRightOf="@id/helper"
    android:orientation="vertical" >
    <Spinner
        android:id="@+id/B"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="2dp"
        android:layout_weight="1"
        android:background="@android:color/black" />
    <Spinner
        android:id="@+id/C"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@android:color/black" />
</LinearLayout>