Edit: I have received already two comprehensive answers regarding fixed margins. While I've decided altogether to use fixed margins instead of weight margins, the original question remains open.
I am trying to obtain the following design in Android:
A centered vertical list of stuff (TextViews, EditViews etc.) which leaves about 10% of the horizontal space free as left/right margin, with background.
What I tried and did not work/worked partially:
(In the last two cases, my Eclipse also complains that one of the layouts is redundant.)
I have not posted code, having considered that this is somewhat more of a principle-related question. What would be the (best) way of accomplishing this?
Thank you.
XML corresponding to the last one of the test case:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:baselineAligned="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:weightSum="1.0"
android:background="#013c57" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:layout_gravity="center"
android:orientation="vertical" >
<!-- Stuff -->
</LinearLayout>
</LinearLayout>
When using the RelativeLayout , you can use android:layout_centerInParent="true" , which will do what it says, center it inside its parent.
To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .
We can set a margin to a JButton by using the setMargin() method of JButton class and pass Insets(int top, int left, int bottom, int right) as an argument.
here is the simplest xml code for creating this type of layout check it
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000"
android:gravity="center">
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
<EditText android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"/>
</LinearLayout>
You can use compile 'com.android.support:percent:24.2.1'
You can now use PercentFrameLayout or PercentRelativeLayout. They both have the following attributes:
layout_widthPercent
layout_heightPercent
layout_marginPercent
layout_marginLeftPercent
layout_marginTopPercent
layout_marginRightPercent
layout_marginBottomPercent
layout_marginStartPercent
layout_marginEndPercent
<?xml version="1.0" encoding="utf-8"?>
<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="match_parent">
<TextView
android:id="@+id/txt1"
android:background="#ff7acfff"
android:text="50% - 50% margin 25"
android:textColor="@android:color/white"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="80%" />
<TextView
android:layout_below="@id/txt1"
android:id="@+id/txt2"
android:background="#ff7acfff"
android:text="50% - 50% margin 25"
android:textColor="@android:color/white"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="80%"/>
<TextView
android:layout_below="@id/txt2"
android:id="@+id/txt3"
android:background="#ff7acfff"
android:text="50% - 50% margin 25"
android:textColor="@android:color/white"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="80%"/>
<TextView
android:layout_below="@id/txt3"
android:id="@+id/txt4"
android:background="#ff7acfff"
android:text="50% - 50% margin 25"
android:textColor="@android:color/white"
app:layout_marginTopPercent="10%"
app:layout_marginLeftPercent="10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="80%"/>
</android.support.percent.PercentRelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With