Android按钮布局 - 在整个屏幕上并排获取两个按钮
我有两个按钮,我想水平并排显示,但它们一起填满了手机的水平长度。高度是包装内容,没关系。我现在的问题是只有一个按钮显示出来(横跨屏幕)。Android按钮布局 - 在整个屏幕上并排获取两个按钮
这里是我的XML代码:
<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包括layout_weight
属性:
<Button android:id="@+id/nextButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next"/>
除了接受的答案之外,我认为值得指出的是,包装这两个按钮的线性布局应该有'android:orientation =“horizontal”'选项。 我很慢,一开始没有理解。 – w3bshark
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/space"
/>
<TextView
android:id="@id/space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/button02"
android:layout_toRightOf="@id/button01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/entry"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
好像你想两个相等的按钮,不包装的内容。我使用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"
/>
<?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>
你应该提供一些解释和代码。 –
由于没有人解释为什么他们的解决方案的工作,我给它一展身手。
问题在于按钮的布局。这两个相关的属性是layout_width和layout_weight。
在其他布局系统中,当您指示布局的每个元素都必须填充父元素(layout_width =“fill_parent”)时,他们通过平均分配父元素之间的空间来实现此目的。所以,他们每个人都会有相同的大小。
相反,Android的布局系统,如果这两个元件具有 layout_width =“FILL_PARENT”的第一个元素(在你的情况下, 预览按钮)将被拉伸以填充父和第二 (或第三或其他等)将不会留下任何空间来分发,因此它不会显示。 将不可见。
现在,为了使它理解您希望两个按钮都显示,您可以为每个按钮设置layout_weight。要使这些按钮具有相同的尺寸,请为它们设置相同的布局权重。
layout_weight定义每个按钮占用的父级的“部分”(或片段)数量。父母会被分成若干段,等于子段的总和。如果您想让一个按钮比另一个大三倍,则必须为其分配与第一个按钮的部件数相等的部件数乘以三。对于下一步按钮layout_weight = 1
所以,如果你希望你的下一个按钮是那么 预览按钮大两倍,你可以这样做:
- 的预览按钮layout_weight = 2
其结果是,亲将在3个部分,其中2个将被分配给Next按钮和1到上一个被切割浏览按钮。
这里采用的示例是按钮和水平布局,但这对任何类型的对象以及垂直布局父项都适用。
非常感谢您的解释 – Joqus
你的按钮的父级是线性布局,并且你将Button的宽度设置为填充父级,因此它占据了整个空间。你有两种选择来实现这个...
- 给你的按钮固定宽度(例如50dip)。
- 给宽度为0dp和插入在这两个按钮“重量”多一个属性,并给每个0.5dip ...
两个按钮应该在一个LinearLayout中。线性布局应该是水平的,每个按钮的权重为1.这应该会给你两个沿着屏幕宽度具有相同大小的按钮。样品在这里 Horizontal orientation: check the 2 Buttons at the end of this xml layout
我发现了仍然似乎没有得到他们想要的人的主要问题。 当使用常规(开箱即用/默认)背景的按钮时,它具有内置保证金。 如果您尝试用单一颜色手动更改背景,可以轻松地告诉您只需设置单独的背景。 除此之外,当你有权重投入时,它会工作。
你究竟如何期待两个按钮出现在屏幕上,因为你已经在xml中指定了一个按钮?你是否动态创建第二个并将其添加到线性布局? – asenovm
我编辑了这篇文章,使其现在可读。 – JDS