简体   繁体   中英

How to make a Button align to the right in a layout?

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>

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>

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>

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"

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM