Android 线性布局中按钮的位置

Android 线性布局中按钮的位置,android,button,android-linearlayout,margin,android-layout-weight,Android,Button,Android Linearlayout,Margin,Android Layout Weight,我已经添加了5个按钮(见下文),我需要重置按钮应该和上面两个按钮一样对齐,以保持一致性。此外,我无法将提交按钮与GoodButton的正下方对齐 我觉得这是由于我在顶部按钮上添加了额外的版面边距空间 <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <

我已经添加了5个按钮(见下文),我需要重置按钮应该和上面两个按钮一样对齐,以保持一致性。此外,我无法将提交按钮与GoodButton的正下方对齐


我觉得这是由于我在顶部按钮上添加了额外的版面边距空间


 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
            <Button android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"></Button>
            <Button android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"></Button>
        </LinearLayout>

        <Button android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_weight="1"
        android:layout_height="match_parent">
        <Button android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></Button>
        <Button android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"></Button>
    </LinearLayout>

</LinearLayout>

使用它,您所需要的正是

使用嵌套权重会带来性能问题。 这种设计可以很容易地通过使用相对布局来实现

检查此代码

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:id="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1"
    android:layout_margin="10dp">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="1" />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.25"
        android:text="2" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:text="3" />
</LinearLayout>

<LinearLayout
    android:layout_below="@+id/layout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" android:orientation="horizontal" android:weightSum="1"
    android:layout_margin="5dp">
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="4" />
    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:text="5" />

</LinearLayout>


您的问题是没有很好地分配视图的权重。 必须对两个水平线性布局应用权重,然后尝试平均分配其子视图的权重

请参见我上面的示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:weightSum="2"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1">


    <Button
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".5"/>

    <Button
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".5" />
    </LinearLayout>


    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Good" />
</LinearLayout>

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


    <Button
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_marginRight="4dp"
        android:layout_weight="1"
        android:text="Reset" />



    <Button
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:text="Submit" />
</LinearLayout>


您也可以使用如下表格布局:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
    <TableRow>
        <Button
            android:layout_marginRight="2dp"
            android:text="1" />
        <Button
            android:layout_marginRight="4dp"
            android:text="1" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />
        <Button
            android:layout_marginLeft="4dp"
            android:layout_weight="1"
            android:text="Good" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_marginRight="4dp"
            android:layout_span="2"
            android:text="Reset" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />
        <Button
            android:layout_marginLeft="4dp"
            android:layout_weight="1"
            android:text="Submit" />
    </TableRow>
</TableLayout>


使用相对线进行精确对齐。您还可以消除布局嵌套嵌套权重可能带来的性能问题,只是深度为2并不重要,当达到完美时,它适用于所有分辨率。设计要简单得多,我希望它不会对他有多大影响。不管怎样,这是约格什的要求……没有给出正确的输出。上面的布局有3个按钮,即使它们的重量相等。下面的一次有点放错了位置…每当我放置一些填充物时,它会变形。我将3个按钮中的2个按钮放置在线性布局内的顶部布局中,并且按钮的重量相等,因此它们将相等。当涉及到填充时,如果解决了您的问题,则对所有按钮给予相等的填充。。你能投票并把它标为答案吗
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="wrap_content">
    <TableRow>
        <Button
            android:layout_marginRight="2dp"
            android:text="1" />
        <Button
            android:layout_marginRight="4dp"
            android:text="1" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />
        <Button
            android:layout_marginLeft="4dp"
            android:layout_weight="1"
            android:text="Good" />
    </TableRow>
    <TableRow>
        <Button
            android:layout_marginRight="4dp"
            android:layout_span="2"
            android:text="Reset" />
        <View
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:background="@android:color/darker_gray" />
        <Button
            android:layout_marginLeft="4dp"
            android:layout_weight="1"
            android:text="Submit" />
    </TableRow>
</TableLayout>