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>
Try This,
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"
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: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.
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
the button will take 50% of total width.
As @hcpl correctly mentioned in his answer:
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:
As a result you get this view:
You can find more details in Building interfaces with ConstraintLayout.