使用权重设置Android相对布局中按钮的宽度

13

我使用了layout_weight参数来将按钮的宽度设置为总布局宽度的70%,但似乎我缺少一些重要细节以使其正常工作。

(另一种解决方案是通过编程方式使用display.getWidth(),但它也无法正常工作,因为如果选择使用button.setWidth()来设置宽度,我不知道我的.xml应该是什么样子。)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent"
    android:layout_weight="1.0">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"  
        android:id="@+id/userVersionTextViewNew"
        android:gravity="center"
        android:layout_centerVertical="true"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"
        android:gravity="center"
        android:layout_above="@id/userVersionTextViewNew"
        android:id="@+id/userSoftSerialNumberTextView"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_200"
        android:layout_above="@id/userSoftSerialNumberTextView"
        android:layout_centerHorizontal="true"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"
        android:gravity="center"
        android:layout_below="@id/userVersionTextViewNew"
        android:id="@+id/dummyTextView"/>       
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/loginButton"
        android:text="Σύνδεση"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/dummyTextView"
        android:layout_weight="0.7"/>
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/demoLoginButton"
        android:text="Δοκιμαστική χρήση"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/loginButton"
        android:layout_weight="0.7"/>
</RelativeLayout>

似乎这可以帮助您:https://dev59.com/fm445IYBdhLWcg3wQX-G - amukhachov
顺便提一下,不建议使用px来设置文本大小,而应该使用sp。 - Ahmed
@Ahmed 谢谢你。我会记住的! :) - iCantSeeSharp
9个回答

28

问题

在RelativeLayout布局中,您无法使用layout_weight参数。这些参数来自LinearLayout。稍后我将在下面提供有关差异的更多信息。但是,首先解决此问题。

解决方案

使用LinearLayout,在其中可以使用权重分配将元素定位在一行中。 但是不要忘记添加layout_weights时使用0dp宽度! 下面的示例显示了70/30的权重分配。

<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent" 
android:layout_height="wrap_content"
android:weightSum="1.0" >

    <Button
        android:text="left" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight=".70" /> 

    <Button
        android:text="right" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:layout_weight=".30" />

</LinearLayout>

在你已经写好的RelativeLayout中,你可以添加所有这些内容。本答案的其余部分是背景信息,每个有此类问题的人都应该阅读以了解他们正在做什么。

RelativeLayout

每当你从一个具有多个元素的布局开始时,我建议你优先选择RelativeLayout而不是线性布局。RelativeLayout非常强大,它可以让您将元素相对于彼此定位(左侧、下方等)。在大多数情况下,这可能就是你需要的全部。

以下是来自Android开发文档的示例(相信我,里面都有):

<?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="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >
    <EditText
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/reminder" />
    <Spinner
        android:id="@+id/dates"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/times" />
    <Spinner
        android:id="@id/times"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/name"
        android:layout_alignParentRight="true" />
    <Button
        android:layout_width="96dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/times"
        android:layout_alignParentRight="true"
        android:text="@string/done" />
</RelativeLayout>

线性布局

虽然LinearLayout看起来很强大,但是如果你想只用线性布局处理所有事情,你很可能会开始嵌套这些布局。这往往会影响性能。

下面是来自安卓开发文档的一个例子。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:orientation="vertical" >
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/to" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/subject" />
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="@string/message" />
    <Button
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/send" />
</LinearLayout>

14

试试这个...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"  
        android:id="@+id/userVersionTextViewNew"
        android:gravity="center"
        android:layout_centerVertical="true"/>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"
        android:gravity="center"
        android:layout_above="@id/userVersionTextViewNew"
        android:id="@+id/userSoftSerialNumberTextView"/>
    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_200"
        android:layout_above="@id/userSoftSerialNumberTextView"
        android:layout_centerHorizontal="true"/>    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="15px"
        android:gravity="center"
        android:layout_below="@id/userVersionTextViewNew"
        android:id="@+id/dummyTextView"/>      
    <LinearLayout  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent" 
        android:gravity = "center_horizontal"
        android:layout_below="@id/dummyTextView"
        android:id="@+id/loginButtonLayout"
        android:weightSum="1.0">  
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/loginButton"
            android:text="Σύνδεση"
            android:layout_weight="0.7"/>
    </LinearLayout>
    <LinearLayout  
        android:layout_height="wrap_content"  
        android:layout_width="fill_parent" 
        android:gravity = "center_horizontal"
        android:layout_below="@id/loginButtonLayout"
        android:weightSum="1.0">  
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/demoLoginButton"
            android:text="Δοκιμαστική χρήση"
            android:layout_weight="0.7"/>
    </LinearLayout>
</RelativeLayout>

它几乎完美运行,第一个按钮比第二个小一点。我怀疑这是文字大小的问题。 - iCantSeeSharp
1
如果要让两个按钮都具有相同的大小,即屏幕宽度的70%,请将按钮的宽度参数更改为android:layout_width =“0dp”。 - chetanmoswal
3
虽然这个答案是正确的,但代码本身并不能自我解释。请看下面更好的综合答案,其中包括代码和详细说明为什么它能够工作。 - Sean Beach

5
我认为在RelativeLayout中不适用layout_weight属性。你可以在RelativeLayout内部添加一个LinearLayout,然后在其中使用layout_weight属性。
此外,在使用layout_weight属性时,通常需要将对象的宽度或高度定义为0dp,因此在您的情况下应该这样做:
android:layout_weight="0.7"
android:layout_height="0dp"

5

我知道这个问题已经老旧了,但是对于那些正在寻找解决方案的人来说:

谷歌推出了一个名为android.support.percent的新API。

PercentRelativeLayout正好符合您的情况:

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent" 
    android:layout_width="fill_parent">

    <!-- Other controls -->

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/loginButton"
        android:text="Σύνδεση"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/dummyTextView"
        app:layout_widthPercent="70%"/>

    <!-- Other controls -->

</android.support.percent.PercentRelativeLayout>

1

layout_weight,在父级LinearLayout上起作用。所以我认为问题就在那里。你必须使用所有线性布局和相对布局的混合来实现你需要的效果。


0

正如@hcpl在他的答案中正确提到的:

你不能在RelativeLayout上使用layout_weight参数。这些是LinearLayout的参数。

没错,他是对的!但是要考虑到嵌套布局所带来的性能负面影响

随着ConstraintLayout的引入,您可以在不使用嵌套LinearLayout的情况下解决问题。您只需粘贴两个垂直指南,分别具有15%和85%的边距,并将按钮放置在它们之间即可。

Layout Editor

这是布局源代码:
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent">

    <TextView
        android:id="@+id/userVersionTextViewNew"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="userVersionTextViewNew"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/userSoftSerialNumberTextView"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/userSoftSerialNumberTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="userSoftSerialNumberTextView"
        app:layout_constraintTop_toBottomOf="@+id/userVersionTextViewNew"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/imageView" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo_200"
        app:layout_constraintTop_toBottomOf="@+id/userSoftSerialNumberTextView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/dummyTextView" />

    <TextView
        android:id="@+id/dummyTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:text="dummyTextView"
        app:layout_constraintTop_toBottomOf="@+id/imageView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/loginButton" />

    <Button
        android:id="@+id/loginButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Σύνδεση"
        app:layout_constraintTop_toBottomOf="@+id/dummyTextView"
        app:layout_constraintLeft_toLeftOf="@+id/leftGuideline"
        app:layout_constraintRight_toLeftOf="@+id/rightGuideline"
        app:layout_constraintBottom_toTopOf="@+id/demoLoginButton" />

    <Button
        android:id="@+id/demoLoginButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Δοκιμαστική χρήση"
        app:layout_constraintTop_toBottomOf="@+id/loginButton"
        app:layout_constraintLeft_toLeftOf="@+id/leftGuideline"
        app:layout_constraintRight_toLeftOf="@+id/rightGuideline"
        app:layout_constraintBottom_toBottomOf="parent" />

    <android.support.constraint.Guideline
        android:id="@+id/leftGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.15" />

    <android.support.constraint.Guideline
        android:id="@+id/rightGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.85" />

</android.support.constraint.ConstraintLayout>

作为结果,你会得到这个视图:

Result view

您可以在使用ConstraintLayout构建界面中找到更多详细信息。


0

我认为如果你想设置按钮的长度而不是使用"wrap_content",就不应该在相对布局标签中定义android:layout_weight="1.0"


android:layout_weight只适用于LinearLayout,不适用于RelativeLayout。 - Thupten

-1

试试这个,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="fill_parent"  
    android:layout_width="fill_parent" 
    android:orientation="vertical"
    android:layout_weight="10"> 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:textSize="15px"   
        android:id="@+id/userVersionTextViewNew" 
        android:layout_weight="0.75"
    /> 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:textSize="15px" 
        android:layout_weight="0.75"
        android:id="@+id/userSoftSerialNumberTextView"/> 
    <ImageView 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:src="@drawable/logo_200" 
        android:layout_weight="0.75"/>     
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:textSize="15px" 
        android:layout_weight="0.75"
        android:id="@+id/dummyTextView"/>        
    <Button 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:id="@+id/loginButton" 
        android:text="Σύνδεση" 
        android:layout_weight="3.5"/> 
    <Button 
        android:layout_width="fill_parent" 
        android:layout_height="0dp" 
        android:id="@+id/demoLoginButton" 
        android:text="Δοκιμαστική χρήση" 
        android:layout_weight="3.5"/> 
</LinearLayout> 

我想你是指7.5而不是0.75。 - iCantSeeSharp
不,我的意思是0.75...如果你希望你的按钮占70%,那么这将为最后2个按钮提供70%,而其余30%则分配给前面4个文本和图像视图。试一下,然后告诉我。 - MKJParekh
以上代码是为高度设置权重,而不是宽度。如果可能,请发布上面的图片以查看XML文件的外观。 - MKJParekh
在我的原始帖子中,我写道按钮宽度为总宽度的70%。但是我的方法无法工作,所以这是关于解决此问题。 - iCantSeeSharp
我可以发布一张屏幕截图,但我认为很明显我想要的是让我的按钮占据总宽度的70%。无论如何,很快就会有屏幕截图。 - iCantSeeSharp
显示剩余2条评论

-2
首先,在容器中添加参数android:weightSum="1.0"(在这种情况下,将其添加到RelativeLayout)。
然后,定义每个子元素的权重。例如,如果您将此参数添加到按钮中:
android:layout_weight="0.5"
android:layout_width="0px"

按钮将占据总宽度的50%。


它不接受android:layout_width="0",应该是"0px",但按钮已经消失了。 - iCantSeeSharp
抱歉,它是“0dp”或“0px”或类似的…… 你必须在所有子元素中设置这些参数。 - vicentazo
所有layout_weight的总和必须等于layout_weightSum。 - vicentazo
RelativeLayout有android:weightSum参数吗? - rahul

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接