【问题标题】:Android layout - Two views next to each other, equal width, use full screen-widthAndroid 布局 - 相邻的两个视图,宽度相等,使用全屏宽度
【发布时间】:2018-05-30 01:36:45
【问题描述】:

我在 android 中的布局有问题。我希望两个视图应该具有相等的宽度,并且它们应该几乎使用屏幕的整个宽度。每个视图都应该包含一个居中的标签。

完成后应该是这样的:

这是我目前所拥有的:

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

        <View
            android:id="@+id/view1"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <View
            android:id="@+id/view2"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp" />

    </RelativeLayout>

我现在只有宽度的占位符值。

谢谢。

【问题讨论】:

  • 尝试在两个视图中使用带有orientation="horizo​​ntal" 和weight=1 的LinearLayout。

标签: android android-layout view android-layout-weight


【解决方案1】:

你必须在 xml 中使用 android:layout_weight 属性,所以试试下面的代码希望它能解决你的问题:-

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

     <Button
        android:text="Register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

  </LinearLayout>

创建一个线性布局,其中每个孩子使用相同数量的 屏幕上的空间,将每个视图的 android:layout_height 设置为 “0dp”(用于垂直布局)或每个视图的 android:layout_width 到“0dp”(水平布局)。然后设置 android:layout_weight 每个视图的“1”

For more info you need to read Linear layout.

【讨论】:

    【解决方案2】:

    使用LinearLayout 会更容易,将布局设置为match_parenthorizontal 方向。

    之后,将layout_width 设置为0pxlayout_weight=1 在您的view 中。

    请参阅此以获得更好的解释:

    Layout buttons so each divides up the space equally

    【讨论】:

      【解决方案3】:

      你必须为此使用 layout_weight 属性。

           <Button
              android:text="Register"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:padding="10dip"
              android:layout_margin="5dp"
              android:layout_weight="1" />
      
           <Button
              android:text="Not this time"
              android:id="@+id/cancel"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:padding="10dip"
              android:layout_margin="5dp"
              android:layout_weight="1" />
      
        </LinearLayout>
      

      【讨论】: