[英]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. 为什么使用TextView:我希望文本视图在switch的左侧或右侧动态移动。所以我同时使用TextView和Switch并动态更改其参数以向左或向右移动文本。
I want output similar to setting Switch's button text attribute. 我希望输出类似于设置Switch的按钮文本属性。
SwitchButtonTF class which presently I'm using: 我目前正在使用的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);
}
}
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
. 只需在textview中添加
android:layout_centerVertical="true"
,因为它位于relativelayout内部。还可以将textview的高度设置为wrap_content
。 So your textview xml should be 所以你的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" />
If you don't have any problem using LinearLayout
in place of RelativeLayout
then can you try this? 如果使用
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>
Edit - try this with RelativeLayout
编辑-使用
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.