![](/img/trans.png)
[英]How to ellipsize center-aligned textview beside a right-aligned button when screen width is small
[英]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.