简体   繁体   English

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

[英]Android relative layout with button width using weight

I've used the layout_weight parameter to set the width of the buttons at 70% of the total layout width, but it seems I'm missing some important detail in order to make it work. 我已经使用layout_weight参数将按钮的宽度设置为总布局宽度的70%,但是为了使它正常工作,我似乎缺少了一些重要的细节。

(Another solution would be to work with display.getWidth() programmatically, but it doesn't work either, because I don't know what my .xml should look like If I choose to set the width with button.setWidth() ) (另一种解决方案是以编程方式使用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>

The Problem 问题

You can't use the layout_weight parameters on a RelativeLayout. 您不能在RelativeLayout上使用layout_weight参数。 These are parameters from the LinearLayout. 这些是LinearLayout中的参数。 I'll give some more information about the differences later below. 稍后,我将提供有关差异的更多信息。 But first about the solution for this question 但是首先关于这个问题的解决方案

A Solution 一个办法

Use a LinearLayout where you can position elements in a row with a weight distribution. 使用LinearLayout,您可以在其中将元素分布在具有权重分布的行中。 Don't forget to use the 0dp width when adding layout_weights though! 不过,在添加layout_weights时,请不要忘记使用0dp宽度! The below example shows a weight distribution of 70/30. 以下示例显示了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>

All this within the RelativeLayout you already had in your code. 您在代码中已经拥有的RelativeLayout中的所有这些。 The rest of this answer is background information that everyone with these questions should read in order to understand what they're doing. 该答案的其余部分是具有这些问题的每个人都应阅读的背景信息,以了解他们在做什么。

RelativeLayout 相对布局

Whenever you start with a layout with more than one element I advise you to prefer a RelativeLayout in favor of the Linear thing. 每当您从一个包含多个元素的布局开始时,我建议您最好使用RelativeLayout来代替Linear元素。 The RelativeLayout is very powerful and lets you position elements in relation to each other (leftOf, below, ...). RelativeLayout非常强大,可让您相对于彼此定位元素(leftOf,下方,...)。 In most cases that is more than you'll ever need. 在大多数情况下,这超出了您的需要。

An example from the android development document (believe me it's all there): 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 线性布局

The LinearLayout might look very capable too but in order to get everything sorted with only Linears you'll most likely start nesting these layouts. LinearLayout看起来也很强大,但是为了使所有内容仅使用Linears进行排序,您很可能会开始嵌套这些布局。 And that's where it get's ugly performance wise. 这就是丑陋的表现明智的地方。

Again an example from the android development documentation . 再次是android开发文档中的示例。

<?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>

Try This.. 尝试这个..

<?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>

I know that this question is old, but just for someone who looking for a solution: 我知道这个问题很旧,但仅适用于寻求解决方案的人:

Google introduced new API called android.support.percent Google引入了名为android.support.percent的新API

PercentRelativeLayout exactly your case: 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>

I don't think layout_weight works inside a RelativeLayout . 我不认为layout_weightRelativeLayout内部工作。 Maybe you should add a LinearLayout inside the RelativeLayout and use layout_weight inside. 也许您应该在RelativeLayout内部添加LinearLayout在其中使用layout_weight

Also when using layout_weight you usually have to have either the width or the height of the object defined as 0dp , so in your case like this: 同样,在使用layout_weight ,通常必须将对象的宽度或高度定义为0dp ,因此在这种情况下,如下所示:

android:layout_weight="0.7"
android:layout_height="0dp"

layout_weight, works on the LinearLayout as parent. layout_weight,在LinearLayout上作为父级工作。 so i think the problem lies there. 所以我认为问题就在这里。 you have to use a mix of all linear layout and relative layouts to achieve what you need. 您必须混合使用所有线性布局和相对布局才能实现所需的功能。

As @hcpl correctly mentioned in his answer: 正如@hcpl在他的答案中正确提到的:

You can't use the layout_weight parameters on a RelativeLayout. 您不能在RelativeLayout上使用layout_weight参数。 These are parameters from the LinearLayout. 这些是LinearLayout中的参数。

Yep, he's right! 是的,他是对的! But think about negative impact on performance caused by nested layouts. 但是请考虑嵌套布局对性能造成的负面影响

With the introduction of ConstraintLayout, you can solve your problem without nested LinearLayout. 通过引入ConstraintLayout,您可以在没有嵌套LinearLayout的情况下解决您的问题。 You just paste two vertical guidelines with 15% and 85% margins and place your buttons between them. 您只需粘贴两个垂直辅助线,边距分别为15%和85%,然后在它们之间放置按钮。

布局编辑器

Here's the layout source code: 这是布局源代码:

<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>

As a result you get this view: 结果,您得到以下视图:

结果视图

You can find more details in Building interfaces with ConstraintLayout . 您可以在使用ConstraintLayout构建接口中找到更多详细信息。

我认为您不应该在相对布局标记中定义android:layout_weight="1.0" ,如果您想设置其他按钮的长度,则需要设置"wrap_content"

Try This, 尝试这个,

<?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> 

First, add the param android:weightSum="1.0" to the container (in this case, add it to RelativeLayout). 首先,将参数android:weightSum="1.0"到容器中(在这种情况下,将其添加到RelativeLayout中)。

Then, define the weight of each of their children. 然后,定义每个孩子的体重。 For example, if you add to a button this 例如,如果您将一个按钮添加到

android:layout_weight="0.5"
android:layout_width="0px"

the button will take 50% of total width. 该按钮将占用总宽度的50%。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM