2

我只需要在屏幕上平行放置两个按钮(每个 50dp 的宽度)。第一个应该留有 10dp 的边距并且放置很好。

但是第二个按钮应该放在屏幕中,从中间(水平)保持 30dp 的marin。我需要做的是它应该从我用箭头指示的地方开始。

在此处输入图像描述

我的设计xml如下。使用 LinearLayout 或 RelativeLayout 都没有关系。

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#634785" >        

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="B1"
        android:layout_marginLeft="10dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="B2"           
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="30dp />
</RelativeLayout>
4

3 回答 3

7

在容器的中心放置一个不可见的视图(0dp 宽和高),并根据需要将第二个按钮相对于它对齐:

<RelativeLayout
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#634785" >        

    <Button
        android:id="@+id/button1"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="B1"
        android:layout_marginLeft="10dp" 
        />

    <Button
        android:id="@+id/container_center"
        android:layout_centerInParent="true"
        android:layout_width="0dp"
        android:layout_height="0dp"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        android:text="B2"           
        android:layout_toRightOf="@+id/container_center" 
        android:layout_marginLeft="30dp"
        />

</RelativeLayout>
于 2013-05-20T11:38:55.867 回答
1
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#634785" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button1"
            android:layout_width="50dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="B1" />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true" >

            <Button
                android:id="@+id/button2"
                android:layout_width="50dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="30dp"
                android:text="B2" />
        </RelativeLayout>
    </RelativeLayout>

</RelativeLayout>
于 2013-05-20T11:44:00.993 回答
0

我尝试了上面的答案,但以上任何一个都不适用于我的情况,我想将一个 button2 放在另一个 button1 上,所以确定 button2 应该从中心放置一些填充,以使其对所有设备都有响应。但是由于某种原因 button2 仍然在左侧。这是我为实现此目的而编写的 XML 代码,请纠正我

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp">
        <Button
            android:id="@+id/EnglishButton"
            android:layout_width="fill_parent"
            android:layout_height="35dp"
            android:text="   English"
            android:textColor="#000000"
            android:gravity="left|center_vertical"
            android:background="@drawable/Color_Button_Language"
            android:layout_marginLeft="20dp" />
        <Button
            android:id="@+id/fakeview"
            android:layout_centerInParent="true"
            android:layout_width="0dp"
            android:layout_height="0dp" />
        <Button
            android:id="@+id/TickButton1"
            android:layout_toRightOf="@+id/fakeview"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/WhiteTick" />
    </RelativeLayout>
    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#6D7077"
        android:layout_marginLeft="20dp" />
</LinearLayout>

这是上述 XML 的结果,但我想在从屏幕中心填充后将刻度放在右侧 在此处输入图像描述

于 2015-09-11T14:26:16.200 回答