Percentage Width

I need to set the width of my view to 50% of the width of the screen, and then center this image horizontally, with the possible presence of 1 or more buttons that can be displayed on the left or right side of the screen.

I use relative layout so that I can place a linear layout with weights to get 50% centering by placing any buttons on top of this LL attached to the left or right edge of the RL. However, there is no blue middle bar in this layout. If I set the average layout_weight layout to 1, I get 3 bars of equal size.

<RelativeLayout android:layout_width="match_parent" android:layout_height="48dp"> <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" /> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="2" /> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" /> </LinearLayout> </RelativeLayout> 
+6
source share
4 answers

You must set the view width to 0dip

 <RelativeLayout android:layout_width="match_parent" android:layout_height="48dp"> <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" > <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" /> <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="2" /> <View android:layout_width="0dip" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1" /> </LinearLayout> </RelativeLayout> 
+15
source

You can achieve this with the new percentage library:

https://developer.android.com/tools/support-library/features.html#percent

by doing something like this:

 <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="48dp"> <View android:layout_width="0dp" android:layout_height="match_parent" android:background="#FF0000" app:layout_widthPercent="25%" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:background="#0000FF" android:centerHorizontal="true" app:layout_marginLeftPercent="25%" app:layout_widthPercent="50%" /> <View android:layout_width="0dp" android:layout_height="match_parent" android:alignParentRight="true" android:background="#00FF00" app:layout_marginLeftPercent="75%" app:layout_widthPercent="25%" /> </android.support.percent.PercentRelativeLayout> 
+1
source

Use the new percentage support library.

 compile 'com.android.support:percent:24.0.0' 

Example:

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/main" android:layout_width="fill_parent" android:layout_height="fill_parent"> <android.support.percent.PercentRelativeLayout android:layout_width="match_parent" android:layout_height="48dp" > <View android:layout_height="match_parent" android:layout_weight="1" android:background="#FF0000" app:layout_widthPercent="33%" /> <View android:layout_height="match_parent" android:layout_weight="2" android:background="#0000FF" app:layout_marginLeftPercent="33%" app:layout_widthPercent="33%" /> <View android:layout_height="match_parent" android:layout_weight="1" android:background="#00FF00" app:layout_marginLeftPercent="66%" app:layout_widthPercent="33%" /> </android.support.percent.PercentRelativeLayout> </LinearLayout> 

For More Information Android Doc

* For example and tutorial * Tutorial1 Tutorial2 Tutorial3

0
source

If I understood correctly, you need this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="48dp"> <LinearLayout android:id="@+id/stupid_android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#FF0000" android:layout_weight="1" ></View> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#0000FF" android:layout_weight="1.5" ></View> <View android:layout_width="fill_parent" android:layout_height="match_parent" android:background="#00FF00" android:layout_weight="1.5" ></View> </LinearLayout> </RelativeLayout> 
-2
source

All Articles