问题
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>
回答1:
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>
回答2:
The Problem
You can't use the layout_weight parameters on a RelativeLayout. These are parameters from the 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. Don't forget to use the 0dp width when adding layout_weights though! The below example shows a weight distribution of 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. 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. The RelativeLayout is very powerful and lets you position elements in relation to each other (leftOf, below, ...). In most cases that is more than you'll ever need.
An example from the android development document (believe me it's all there):
<?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. And that's where it get's ugly performance wise.
Again an example from the android development documentation.
<?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>
回答3:
I don't think layout_weight
works inside a RelativeLayout
. Maybe you should add a LinearLayout
inside the RelativeLayout
and use layout_weight
inside.
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:
android:layout_weight="0.7"
android:layout_height="0dp"
回答4:
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>
回答5:
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.
回答6:
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"
回答7:
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.
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.
回答8:
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>
回答9:
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.
来源:https://stackoverflow.com/questions/7846614/android-relative-layout-with-button-width-using-weight