Android,线性布局中按钮的垂直对齐

时间:2016-10-05 16:05:46

标签: android user-interface android-linearlayout

我正在尝试创建如下图所示的布局,以适应屏幕尺寸。 AdaptiveLayout

这是我的XML源代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LnrLytRow1"
        android:weightSum="100"
        android:layout_marginLeft="25dp"
        android:layout_marginRight="25dp">
        <Button
            android:text="Button1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button1"
            android:width="0dp"
            android:layout_weight="25" />
        <Button
            android:text="Button2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button2"
            android:width="0dp"
            android:layout_weight="25" />
        <Button
            android:text="Button3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button3"
            android:width="0dp"
            android:layout_weight="25" />
        <Button
            android:text="Button4"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button4"
            android:width="0dp"
            android:layout_weight="25" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/LnrLytRow2"
        android:weightSum="100"
        android:layout_marginRight="25dp"
        android:layout_marginLeft="25dp">
        <Button
            android:text="Button5"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button5"
            android:width="0dp"
            android:layout_weight="25" />
        <Button
            android:text="Button6"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button6"
            android:width="0dp"
            android:layout_weight="25" />
        <Button
            android:text="Button7"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button7"
            android:width="0dp"
            android:layout_weight="50" />
    </LinearLayout>
</LinearLayout>

这是我得到的: enter image description here

如图所示,按钮未正确对齐。

任何建议都将受到赞赏。

3 个答案:

答案 0 :(得分:3)

您应该切换到GridLayout

它旨在满足您的需求。它基于行和列,您可以指定元素的行和列间距。更不用说它将为您提供更好的性能,然后嵌套LinearLayout s。

答案 1 :(得分:0)

您应该更改第二个LinearLayout的宽度,并从这些按钮中删除android:width属性

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
    android:orientation="horizontal"
    android:minWidth="25px"
    android:minHeight="25px"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/LnrLytRow1"
    android:weightSum="100"
    android:layout_marginLeft="25dp"
    android:layout_marginRight="25dp">
    <Button
        android:text="Button1"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button1"
        android:width="0dp"
        android:layout_weight="25" />
    <Button
        android:text="Button2"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button2"
        android:width="0dp"
        android:layout_weight="25" />
    <Button
        android:text="Button3"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button3"
        android:width="0dp"
        android:layout_weight="25" />
    <Button
        android:text="Button4"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/button4"
        android:width="0dp"
        android:layout_weight="25" />
</LinearLayout>
<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minWidth="25px"
    android:minHeight="25px"
    android:id="@+id/LnrLytRow2"
    android:weightSum="100"
    android:layout_marginRight="25dp"
    android:layout_marginLeft="25dp">
    <Button
        android:text="Button5"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button5"
        android:layout_weight="25" />
    <Button
        android:text="Button6"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button6"
        android:layout_weight="25" />
    <Button
        android:text="Button7"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:id="@+id/button7"
        android:layout_weight="50" />
</LinearLayout>

答案 2 :(得分:0)

在你的build.gradle中(任何版本都可以):

compile "com.android.support:gridlayout-v7:+"

将此代码复制到xml文件中

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:columnCount="4"
    app:rowCount="2">

    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"/>

    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"/>

    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"/>

    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button4"/>

    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button5"/>
    <Button
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button6"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button7"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        app:layout_columnSpan="2"/>

</android.support.v7.widget.GridLayout>

查看证明

enter image description here