【问题标题】:android: relative layout two buttons occupy all the available horizontal spaceandroid:相对布局两个按钮占据所有可用的水平空间
【发布时间】:2011-03-14 19:00:06
【问题描述】:

我有一个相对布局,其中有两个按钮,分别带有文本“hello”和“world”。我希望这两个按钮彼此相邻,并平等地占据整个可用的水平空间。

我尝试了以下但没有得到预期的输出

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

     <Button android:layout_height="wrap_content" android:layout_width="wrap_content"      android:text="@string/world" android:id="@+id/world"
     android:layout_alignParentRight="true"
     android:layout_toRightOf="@+id/hello"
     android:layout_alignTop="@+id/hello"
  />  


       <Button
          android:id="@+id/hello"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/hello"
          android:layout_alignParentLeft="true"
          android:layout_alignParentBottom="true"
        />
   </RelativeLayout>

我尝试将两个孩子的 android:layout_width 更改为 fill_parent 但这也没有用。

我有一个可行的解决方案,即在两个孩子上使用 LinearLayout 并将 layout_weight 设置为 0.5,但我想了解是否有办法在相对布局本身中做到这一点。

谢谢,

【问题讨论】:

  • 您似乎已经有了解决方案 - 您到底在寻找什么?
  • 使用带有 layout_weight 的 LinearLayout。这是公认的方法。
  • 我认为有一个比你选择的更好的答案

标签: android android-layout


【解决方案1】:
  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/hello"
        android:layout_toRightOf="@+id/view"
        android:text="First" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true" />

    <Button
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/view"
        android:text="Second" />

</RelativeLayout>

注意:

  • 即使android:layout_widthfill_parent,结果也不会改变。
  • 这里真正重要的是android:layout_alignParentRightandroid:layout_alignParentLeft

【讨论】:

  • 我很好奇为什么这在RelativeLayout 中有效。为什么按钮占用的空间比它需要的多(wrap_content)?
  • @dannyroa android:layout_width 无所谓,即使是fill_parent 结果也不会改变,真正重要的是android:layout_alignParentRight="true"android:layout_alignParentLeft="true"
  • 我猜这与RelativeLayout的布局逻辑有关。但这是一个很好的解决方法。
  • 对于大于16的版本,还需要加上android:layout_alignParentStart="true" 和android:layout_alignParentEnd="true"
【解决方案2】:

RelativeLayout 无法做到这一点。试试LinearLayoutlayout_weight 属性。

【讨论】:

    【解决方案3】:

    我知道这是一个旧请求,但也许它对某人有用。

    在您的情况下,正确的解决方案是使用线性布局。但是为了回复您的请求并假设您的布局中有其他小部件:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
    
     <TextView
        android:id="@+id/tvdescription"        
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:text="description here"/>
    
     <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/tvdescription">
    
        <Button          
            android:id="@+id/world"
            android:layout_height="wrap_content" 
            android:layout_width="0dp"      
            android:text="@string/world"
            android:weight="1"
        />  
       <Button
            android:id="@+id/hello"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/hello"
            android:weight="1"
        />
    </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      您可以尝试实现 LinearLayout(horizo​​ntal),然后为两个按钮设置 android:layout_weight="1"。

      【讨论】:

        【解决方案5】:
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        
        int height = metrics.heightPixels;
        int width = metrics.widthPixels;
        

        在相对布局中,您可以动态实现。

        Button1.setWidth(width / 2);
        Button2.setWidth(width / 2);
        

        【讨论】:

          【解决方案6】:

          选择一个按钮,将其与1dp widthheight 对齐。现在将您的 Button1 与属性 android:layout_width = fill parentto left of center 一起放置,同样关于中心按钮将 Button2 放置到具有属性 android:layout_width = fill parentright of center

          【讨论】:

            猜你喜欢
            • 2012-09-26
            • 1970-01-01
            • 1970-01-01
            • 2020-11-07
            • 1970-01-01
            • 2021-12-10
            • 2017-04-05
            • 2015-08-11
            • 2013-07-19
            相关资源
            最近更新 更多