Android relative layout with button width using we

2019-01-18 04:32发布

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.

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

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

9条回答
smile是对你的礼貌
2楼-- · 2019-01-18 05:09

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> 
查看更多
欢心
3楼-- · 2019-01-18 05:10

I think you should not define android:layout_weight="1.0" in Relative layout tag, if you want to set the length of button other then the "wrap_content"

查看更多
唯我独甜
4楼-- · 2019-01-18 05:13

I know that this question is old, but just for someone who looking for a solution:

Google introduced new API called android.support.percent

PercentRelativeLayout exactly your case:

<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
5楼-- · 2019-01-18 05:14

layout_weight, works on the LinearLayout as parent. 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.

查看更多
Emotional °昔
6楼-- · 2019-01-18 05:16

First, add the param android:weightSum="1.0" to the container (in this case, add it to 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.

查看更多
戒情不戒烟
7楼-- · 2019-01-18 05:18

As @hcpl correctly mentioned in his answer:

You can't use the layout_weight parameters on a RelativeLayout. These are parameters from the 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. You just paste two vertical guidelines with 15% and 85% margins and place your buttons between them.

Layout Editor

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:

Result view

You can find more details in Building interfaces with ConstraintLayout.

查看更多
登录 后发表回答