Android xml 中 RelativeLayout 中两个按钮之间的间距

Spacing between two buttons in RelativeLayout in Android xml

我有一个包含两个按钮的相对布局。我正在尝试动态设置这两者的相对位置,以便在不同的屏幕尺寸上看起来相同。布局的位置已根据屏幕尺寸正确设置,但按钮之间的间距未设置。

<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/buttonLine"
        android:layout_above="@+id/separator">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:background="@drawable/skip"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="60dp"/>



    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonClr"
        android:background="@drawable/clear"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_marginRight="60dp"/>

    </RelativeLayout>

在我的 java class 中,我有一个动态部分的方法。我将这段代码用于此按钮相对布局。

button_line.getLayoutParams().height = getHorizontalRatio(40); //button_line is RelativeLayout
 skip.setPadding(getHorizontalRatio(40), 0, 0, 0); //skip and clear are the buttons
 clear.setPadding(getHorizontalRatio(400), 0, 0, 0);
skip.getLayoutParams().width = getHorizontalRatio(75);
            skip.getLayoutParams().height = getHorizontalRatio(25);
            clear.getLayoutParams().width = getHorizontalRatio(75);
            clear.getLayoutParams().height = getHorizontalRatio(25);

PS: 我试过改变比例,把布局改成LinearLayout(我完全没看懂)。请提出一些建议。

Android 自动处理元素相对于屏幕尺寸的定位。 我们使用 dp(密度像素)作为单位来给出边距或填充,在 return 中将根据设备的屏幕尺寸放置元素。

查看此 link 了解更多详情: http://developer.android.com/guide/practices/screens_support.html

因此,总而言之,您不必在运行时设置内边距,您可以在布局中进行更改 xml。

编辑尝试使用我的一个工作项目中的片段!!..

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="15dp"
    android:orientation="horizontal">
 <Button
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="120dp"
     android:text="CLEAR"
     android:id="@+id/clearBtn"/>
 <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="SEARCH"
        android:id="@+id/searchBtn"/>
</LinearLayout>

希望这有效..

性能不是很好,但工作正常:)

<LinearLayout 
    android:id="@+id/buttonLine"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/separator"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center" >

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/skip" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:gravity="center" >

        <Button
            android:id="@+id/buttonClr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/clear" />
    </LinearLayout>

</LinearLayout>

试试这个...

更多详情:Layout Weight

<LinearLayout
    android:id="@+id/layout_id"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/clear_btn"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/ic_action_clear"
        android:padding="8dp" />

    <Button
        android:id="@+id/skip_btn"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/ic_action_skip"
        android:padding="8dp" />
</LinearLayout>

可能是这样的

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp">

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:background="@drawable/skip" />

   <View
       android:layout_width="0dp"
       android:layout_height="wrap_content"
       android:layout_weight="1"/>

   <Button
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/buttonClr"
       android:background="@drawable/clear />
</LinearLayout>

这是你想要的吗?

<?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="horizontal"
              android:padding="20dp"
              android:weightSum="2">

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp"
        android:layout_weight="1"
        android:text="Button 1"/>

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_weight="1"
        android:text="Button 2"/>

</LinearLayout>

尝试使用 TableLayout 并使用权重 属性 来调整您需要的间距。您的按钮之间的 TextView 将用作分隔符。

<TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <TableRow
              android:id="@+id/tableRow1"
              android:layout_height="wrap_content"
              android:layout_width="match_parent"
              android:weightSum = 11>

              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/button"
                  android:layout_weight="5"
                  android:background="@drawable/skip"/>

              <TextView
                  android:id="@+id/TextView04" 
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_weight="1"/>

              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/buttonClr"
                  android:layout_weight="5"
                  android:background="@drawable/clear"/>

        </TableRow>

</TableLayout>

在您的 RelativeLayout 中添加此 table 布局。

希望对您有所帮助。