Android Left,Center and Right Alignment
I am pretty new to android and trying to do a sample app where 3 elements (2 buttons and 1 text view ) are placed horizontally. I need the text to be in the center with two buttons aligned left and right of the view.
E.g.
|<Button> <Text> <Button>|
A开发者_运维百科ppreciate if you could help me with this.
Thanks in advance
EDIT: I believe this will solve your problem. See the below code. Let me know if this helps! I know this has helped me and i will be using it. Accept this as the answer if it worked! =)
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:id="@+id/button2button"></Button>
</TableRow>
</TableLayout>
EDIT: Added relative layout to see if it works for you, let me know if it works. You will need to adjust the "10px" to get the desired distance. I am seeing if there is a better way to do this, where the distance is more dynamic.
You can make the buttons "alignParentLeft" and "alignParentRight" to get them to align with the right and left side of the screen. However, i am still having trouble getting the text between the two views.
You could do this in your xml file for the layout. Make it a horozontal layout and add the first button first, then the text view, then the second button. Will post xml shortly for an example.
<RelativeLayout
android:id="@+id/LinearLayout01"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"
android:layout_marginRight="10px"
android:id="@+id/button1button"></Button>
<EditText
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_toRightOf="@id/button1button"
android:layout_marginRight="10px"
android:layout_width="wrap_content"></EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/firstedittext"
android:text="button2"
android:id="@+id/button2button"></Button>
</RelativeLayout>
this will do the trick. What you need is absolute gravity. This one should work with all Android screen sizes too. I don't think you will need to use any margins or paddings, they will mess you up once you have contents in your children (margins are relative in terms of choosing them, aka: you want a smaller margin when you have less text...which happens often during translation, new functions, replacements, etc..)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_alignParentLeft="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/firstedittext"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_alignParentRight="true"/>
</RelativeLayout>
This is my code on LinearLayout. Hope it help you!
<?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="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="Button1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ems="10"
android:text="Chao cac ban" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Button2" />
</LinearLayout>
</LinearLayout>
精彩评论