Android布局 - 两个视图彼此相邻,宽度相等,使用全屏宽度

Mik*_*lty 4 android view android-layout android-layout-weight

我在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>
Run Code Online (Sandbox Code Playgroud)

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

谢谢.

dug*_*ggu 10

你必须在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>
Run Code Online (Sandbox Code Playgroud)

要创建一个线性布局,其中每个子节点在屏幕上使用相同的空间量,请将每个视图的android:layout_height设置为"0dp"(对于垂直布局)或将每个视图的android:layout_width设置为"0dp"(用于水平布局).然后将每个视图的android:layout_weight设置为"1"

有关更多信息,您需要阅读线性布局.