Android按钮布局-在整个屏幕上并排放置两个按钮

问题描述 投票:7回答:8

我有两个按钮,我希望水平并排显示,但是它们一起占据了手机的水平长度。高度是包装内容,没关系。我现在的问题是仅显示一个按钮(在屏幕上伸展)。

这是我的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"
   />

android xml button android-linearlayout
8个回答
24
投票

更改您的Buttons 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"/>

13
投票

由于没有人解释他们的解决方案为何有效,所以我会努力的。

问题在于按钮的布局。有关的两个属性是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个分配给Previews按钮。

这里采用的示例是针对按钮和水平布局的,但这对于任何类型的对象以及垂直布局父对象都适用。


5
投票
<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创建了居中的垫片,并使其相对对齐。向左键移至父级左侧和空格,向右键移至Left Button并右侧父级。


1
投票

您的按钮的父对象是线性布局,您将按钮的宽度设置为填充父对象,因此它占用了整个空间。您有两种选择可以实现此目标...


1
投票

这两个按钮应位于线性布局上。线性布局应为水平,每个按钮的权重为1。这将为您提供两个沿屏幕宽度大小相等的按钮。样品在这里Horizontal orientation: check the 2 Buttons at the end of this xml layout


0
投票

您的要求是


0
投票
<?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> 

0
投票

我发现了似乎仍然无法获得所需东西的人们的头等大事。当使用常规(开箱即用/默认)背景的按钮时,它具有内置边距。如果您尝试用一种颜色手动更改背景,则可以很容易地说出您只需要设置一个单独的背景。除此之外,当您添加权重时,它将起作用。

© www.soinside.com 2019 - 2024. All rights reserved.