Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在线性布局中水平对齐图元?_Android_Android Layout_Android Linearlayout_Android Layout Weight - Fatal编程技术网

Android 如何在线性布局中水平对齐图元?

Android 如何在线性布局中水平对齐图元?,android,android-layout,android-linearlayout,android-layout-weight,Android,Android Layout,Android Linearlayout,Android Layout Weight,我无法控制线性布局的高度。这些不会正确对齐,也不会填满宽度。我希望分隔器在中间,两个按钮在两边。这是我的密码: <LinearLayout android:id="@+id/buttonFieldsLayout" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout

我无法控制
线性布局的高度。这些不会正确对齐,也不会填满宽度。我希望分隔器在中间,两个按钮在两边。这是我的密码:

<LinearLayout
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

    <Button
        android:id="@+id/signUpButton"
        style="@style/AuthButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sign_up_button_label" />

    <View
        android:id="@+id/buttonDivider"
        android:layout_width="1dp"
        android:layout_height="wrap_content"
        android:background="@drawable/divider" />

    <Button
        android:id="@+id/cancelButton"
        style="@style/AuthButton"
        android:text="@string/cancel_button_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
注意:
android:divider
属性仅在
android
3.0(API级别11)或更高版本中可用


实际上,这个链接帮助了我,并展示了放置分隔符的正确方法:

使用RelativeLayout,而使用android:layout\u width=“match\u parent”

对于分隔器,设置
android:layout\u centerHorizontal=“true”

对于按钮,设置:

android:layout_toRightOf="@id/buttonDivider"
……还有

android:layout_toLeftOf="@id/buttonDivider"

对于
按钮
,您需要使用
layout\u weight
属性。要防止分隔符填充版面高度(如图片中屏幕的整个高度),请使用
layout\u height=“match\u parent”
而不是
layout\u height=“wrap\u content”
。这将填充屏幕(或父屏幕)的宽度,并将
按钮的
宽度设置为相等,其间的
分隔符
高度相同:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

<Button
    android:id="@+id/signUpButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sign_up_button_label" />

<View
    android:id="@+id/buttonDivider"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@drawable/divider" />

<Button
    android:id="@+id/cancelButton"
    android:text="cancel_button_label"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />


如果您这样做,将外部线性布局设置为
layout\u width=“fill\u parent”
和内部按钮设置为
layout\u width=“0dp”
,您应该很好。如果这让你陷入困境,我希望看到代码,并将很乐意进一步提供帮助。
android:layout_toLeftOf="@id/buttonDivider"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/buttonFieldsLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/loginFieldsLayout" >

<Button
    android:id="@+id/signUpButton"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="sign_up_button_label" />

<View
    android:id="@+id/buttonDivider"
    android:layout_width="1dp"
    android:layout_height="match_parent"
    android:background="@drawable/divider" />

<Button
    android:id="@+id/cancelButton"
    android:text="cancel_button_label"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content" />