简体   繁体   中英

how can switch's custom right textview be center aligned?

This is my layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/form_widget_switch_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toEndOf="@+id/form_widget_switch"
        android:gravity="center"
        android:text="text" />

    <com.example.daffodil.formvalidation.widgets.typefaced.SwitchButtonTF
        android:id="@+id/form_widget_switch"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:switchMinWidth="40dp"
        android:textOff=""
        android:textOn=""
        android:thumbTextPadding="5dp" />
</RelativeLayout>

This is the output I'm getting:

在此处输入图片说明

Problem : I want text to be center vertically aligned but as you can see its top edge is aligned with top edge of the switch.

Edit: based on comments

Why am I using TextView : I want text view to move dynamically left or right side of the switch.so I use both TextView and Switch and changing their params dynamically to move text left or right.

I want output similar to setting Switch's button text attribute.

SwitchButtonTF class which presently I'm using:

/**
 * Switch with typeface attribute added.
 */
public class SwitchButtonTF extends Switch {

    /**
     * {@inheritDoc}
     */
    public SwitchButtonTF(Context context) {
        super(context);
        init(context, null);
    }

    /**
     * Initialise view.
     *
     * @param context context
     * @param attrs   layout attributes
     */
    private void init(Context context, AttributeSet attrs) {
        if (!isInEditMode()) {
            Fonts.getInstance(context).applyTypeFace(this, context, attrs);
        }
    }

    /**
     * {@inheritDoc}
     */
    public SwitchButtonTF(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    /**
     * {@inheritDoc}
     */
    public SwitchButtonTF(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }
}

Any help?

Just add android:layout_centerVertical="true" in your textview as it is inside the relativelayout.Also set the height of the textview to wrap_content . So your textview xml should be

<TextView
        android:id="@+id/form_widget_switch_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/form_widget_switch"
        android:gravity="center"
        android:layout_centerVertical="true"
        android:text="text" />

If you don't have any problem using LinearLayout in place of RelativeLayout then can you try this?

<?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="56dp"
        android:orientation="horizontal">    
        <com.example.daffodil.formvalidation.widgets.typefaced.SwitchButtonTF
            android:id="@+id/form_widget_switch"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:switchMinWidth="40dp"
            android:textOff=""
            android:textOn=""
            android:thumbTextPadding="5dp" />

        <TextView
            android:id="@+id/form_widget_switch_text"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center|center_vertical"
            android:text="text" />
    </LinearLayout>

Edit - try this with RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:gravity="center_vertical">
    <TextView
        android:id="@+id/form_widget_switch_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/form_widget_switch"
        android:gravity="center"
        android:text="text" />

    <com.example.daffodil.formvalidation.widgets.typefaced.SwitchButtonTF
        android:id="@+id/form_widget_switch"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:switchMinWidth="40dp"
        android:textOff=""
        android:textOn=""
        android:thumbTextPadding="5dp" />
</RelativeLayout>

Try this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/form_widget_switch_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/form_widget_switch"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="text" />

    <com.example.daffodil.formvalidation.widgets.typefaced.SwitchButtonTF
        android:id="@+id/form_widget_switch"
        android:layout_width="wrap_content"
        android:layout_height="56dp"
        android:gravity="center"
        android:switchMinWidth="40dp"
        android:textOff=""
        android:textOn=""
        android:thumbTextPadding="5dp" />
</RelativeLayout>

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