Android,线性布局中按钮的垂直对齐
我正在尝试创建一个类似下图的布局,以适应屏幕大小。 以下是我的XML源代码:Android,线性布局中按钮的垂直对齐,android,user-interface,android-linearlayout,Android,User Interface,Android Linearlayout,我正在尝试创建一个类似下图的布局,以适应屏幕大小。 以下是我的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:la
<?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>
下面是我得到的:
如图所示,按钮未正确对齐
如果您有任何建议,我们将不胜感激。您应该转到
它的设计正是为了满足你的需要。它基于行和列,您可以指定元素的行和列跨度。更不用说它会比嵌套LinearLayout
s提供更好的性能。您应该切换到
它的设计正是为了满足你的需要。它基于行和列,您可以指定元素的行和列跨度。更不用说它比嵌套
LinearLayout
s提供更好的性能。您应该更改第二个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>
您应该更改第二个线性布局的宽度,并从这些按钮中删除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>
在build.gradle中(任何版本都可以):
将此代码复制到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>
在build.gradle中(任何版本都可以):
将此代码复制到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>
尝试将所有按钮上的android:layou width
更改为0dp
@Kevinrob我的代码中已经有了它android:width
不是android:layout\u width
@Kevinrob你说得对。对不起,我误会了。它起作用了。谢谢尝试将所有按钮上的android:layou width
更改为0dp
@Kevinrob我的代码中已经有了android:width
不是android:layout\u width
@Kevinrob你说得对。对不起,我误会了。它起作用了。谢谢谢谢你的提示。GridLayout的问题是我不能将按钮正确地安装在布局中。最后一列的右侧(按钮4和7)超出了GridLayout的边界。请确保未设置按钮的宽度和高度。GridLayout
使用重力设置来确定视图的尺寸。感谢您的提示。GridLayout的问题是我不能将按钮正确地安装在布局中。最后一列的右侧(按钮4和7)超出了GridLayout的边界。请确保未设置按钮的宽度和高度。GridLayout
使用重力设置来确定视图的尺寸。