簡體   English   中英

開關的自定義右文本視圖如何居中對齊?

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

這是我的布局文件:

<?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>

這是我得到的輸出:

在此處輸入圖片說明

問題:我希望文本垂直居中對齊,但是正如您所看到的,其頂部邊緣與開關的頂部邊緣對齊。

編輯:基於評論

為什么使用TextView:我希望文本視圖在switch的左側或右側動態移動。所以我同時使用TextView和Switch並動態更改其參數以向左或向右移動文本。

我希望輸出類似於設置Switch的按鈕文本屬性。

我目前正在使用的SwitchButtonTF類:

/**
 * 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);
    }
}

有什么幫助嗎?

只需在textview中添加android:layout_centerVertical="true" ,因為它位於relativelayout內部。還可以將textview的高度設置為wrap_content 所以你的textview xml應該是

<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" />

如果使用LinearLayout代替RelativeLayout沒問題,那么您可以嘗試一下嗎?

<?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>

編輯-使用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>

嘗試這個:

<?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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM