Android按钮布局-在整个屏幕上并排放置两个按钮
我有两个按钮,我想在水平方向并排出现,但它们一起填充了手机的水平长度。高度是包裹内容,这很好。我现在的问题是只显示一个按钮(在屏幕上延伸) 以下是我的XML代码:Android按钮布局-在整个屏幕上并排放置两个按钮,android,xml,button,android-linearlayout,Android,Xml,Button,Android Linearlayout,我有两个按钮,我想在水平方向并排出现,但它们一起填充了手机的水平长度。高度是包裹内容,这很好。我现在的问题是只显示一个按钮(在屏幕上延伸) 以下是我的XML代码: <LinearLayout android:id="@+id/page_buttons" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"
<LinearLayout
android:id="@+id/page_buttons"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/prevButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="@+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Next"
/>
更改按钮XML以包含
布局权重
属性:
<Button android:id="@+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next"/>
似乎您需要两个相等的按钮,而不是包装内容。我使用TextView创建了一个居中的间隔符,并与之相对对齐。左键到左键和间隔键,右键到左键和右键。您的要求是
<LinearLayout
android:id="@+id/page_buttons"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/prevButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="@+id/nextButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Next"
/>
既然没有人解释他们的解决方案为什么有效,我就试试看 问题在于按钮的布局。两个相关属性是布局宽度和布局重量 在其他布局系统中,当您指示布局的每个元素都必须填充父元素(layout_width=“fill_parent”)时,它们通过在它们之间平均分配父元素的空间来完成填充。所以,它们的大小都是一样的 相反,在Android的布局系统中,如果两个元素都有 layout\u width=“fill\u parent”第一个元素 预览按钮)将被拉伸以填充父级和第二级 (或第三,等)将没有任何空间可供分配,因此 将不可见 现在,为了让它明白您希望两个按钮都显示,您可以为每个按钮设置布局权重。要使按钮具有相同的大小,请将两个按钮的布局权重设置为相同 布局权重定义每个按钮的父按钮的“部分”(或段)数量。父对象将被切割成若干段,这些段等于子对象段的总和。如果要使一个按钮比另一个按钮大三倍,则必须将第一个按钮的零件数乘以三 所以如果你想让你的下一个按钮比 “预览”按钮,您可以执行以下操作:
- 预览按钮:布局\权重=1
- 下一步按钮:布局\重量=2
这里的示例适用于按钮和水平布局,但这对于任何类型的对象和垂直布局父对象都适用。按钮的父对象是线性布局,您将按钮的宽度设置为填充父对象,因此它占用了整个空间。你有两个选择来实现这个
这两个按钮应位于线性布局上。线性布局应为水平布局,每个按钮的重量为1。这将为您提供沿屏幕宽度大小相等的两个按钮。这里有一个样品
我发现,对于那些似乎仍然得不到想要的东西的人来说,这是一个主要问题。 当使用具有传统(开箱即用/默认)背景的按钮时,它具有内置边距。 如果您尝试用一种颜色手动更改背景,您可以很容易地看出您只需要设置一个单独的背景。
除此之外,当您输入权重时,它会起作用。如果您在xml中只指定了一个按钮,那么您希望屏幕上会出现两个按钮吗?你是在动态创建第二个按钮并将其添加到线性布局中吗?我编辑了这篇文章以使其可读。除了公认的答案之外,我认为值得指出的是,包装这两个按钮的线性布局应该具有选项
android:orientation=“horizontal”
。我很慢,一开始没听清楚。非常感谢你的解释
<LinearLayout
android:id="@+id/page_buttons"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/prevButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="@+id/nextButton"
android:layout_width="0dp"
android:weight="0.5"
android:layout_height="wrap_content"
android:text="Next"
/>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<TextView
android:text="@+id/SomeText"
android:id="@+id/TextView01"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:background="@android:drawable/bottom_bar"
android:paddingLeft="4.0dip"
android:paddingTop="5.0dip"
android:paddingRight="4.0dip"
android:paddingBottom="1.0dip"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_below="@+id/TextView01">
<Button
android:id="@+id/allow"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Allow"
android:layout_weight="1.0" />
<Button
android:id="@+id/deny"
android:layout_width="0.0dip" android:layout_height="fill_parent"
android:text="Deny"
android:layout_weight="1.0" />
</LinearLayout>
</RelativeLayout>