Reputation: 60909
I have a custom ListItem row that has a TextView on the top and a TextView on the bottom. I would like to create a Button that is right aligned, and in centered vertically. How can I do this in XML?
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18sp" android:textColor="#FFFFFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15sp" android:textColor="#FFFFFF"/>
</LinearLayout>
</LinearLayout>
Upvotes: 4
Views: 28926
Reputation: 24732
If you use RelativeLayout try
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
That works for me but took a lot of tweaking to get right.
And you also may need
android:layout_width="wrap_content"
Upvotes: 1
Reputation: 40401
Use android:gravity
or setGravity(int)
Specifies how to place the content of an object, both on the x- and y-axis, within the object itself.
http://developer.android.com/reference/android/widget/LinearLayout.html#attr_android:gravity
specifically for this case, right|center_vertical
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right|center_vertical">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="text centered"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/bottomtext"
android:text="text to the left"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="the button is at the right"/>
</LinearLayout>
Upvotes: 4
Reputation: 7350
i was thinking he wanted something like this:
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<Button android:text="@+id/Button01" android:id="@+id/Button01"
android:layout_width="wrap_content" android:layout_alignParentRight="true"
android:layout_centerVertical="true" android:layout_height="wrap_content" />
<LinearLayout android:orientation="vertical"
android:layout_toLeftOf="@+id/Button01" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/TextLayout">
<TextView android:id="@+id/toptext" android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1"
android:gravity="center_vertical" android:textSize="18sp"
android:textColor="#FFFFFF" android:background="#FFAABB" />
<TextView android:layout_width="fill_parent"
android:layout_height="0dip" android:layout_weight="1" android:id="@+id/bottomtext"
android:singleLine="true" android:ellipsize="marquee"
android:textSize="15sp" android:textColor="#FFFFFF"
android:background="#FFBBAACC" />
</LinearLayout>
</RelativeLayout>
Upvotes: 1
Reputation: 4907
You can achieve this with something similar to the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
android:textSize="18sp" android:textColor="#FFFFFF"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="@+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
android:textSize="15sp" android:textColor="#FFFFFF"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right|center_vertical"
/>
</LinearLayout>
</LinearLayout>
Upvotes: 0