简体   繁体   English

如何在Android开关小部件中包装冗长的文本

[英]How to wrap a lengthy text in an android switch widget

I have a custom style for a switch widget in my app. 我的应用程序中有一个开关小部件的自定义样式。 There is text for both, textOff and textOn. 同时存在textOff和textOn的文本。 The text that is shown is short in English, but unfortunately long or even contains more than one word in other languages and therefore does not fit into the widget. 所显示的文本为英语简短,但不幸的是,该文本很长甚至包含多个其他语言的单词,因此不适合该小部件。 This causes the widget's drawable be pushed out of the widget's visible area and sometimes the switch button itself does not fit in. 这会导致将窗口小部件的可绘制对象推出窗口小部件的可见区域,并且有时开关按钮本身无法放入其中。

(unfortunately I cannot post images due to lack of reputation) (不幸的是,由于缺乏声誉,我无法发布图片)

I want the text to be wrapped if the text does not fit anymore. 如果文本不再适合,我希望将文本换行。 Is this possible? 这可能吗? I tried singleLine="false" as well as the ellipsize attribute and both do not show any effect. 我尝试了singleLine =“ false”以及ellipsize属性,但两者均未显示任何效果。

<Switch
  style="@style/MarginedElement"
  android:id="@+id/toggle_button"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:drawableStart="@drawable/toggle_switch"
  android:textOff="@string/textOff"
  android:textOn="@string/textOn"
  android:thumb="@drawable/click_style_switch_button_thumb"
  android:track="@drawable/click_style_switch_button_bg"
  android:background="@drawable/click_style_switch_bg"
  android:switchTextAppearance="@style/switchText"
 />

Any idea how I could go about this? 知道我该怎么做吗? Or alternative solutions besides changing the text itself. 或者,除了更改文本本身以外,还可以使用其他解决方案。

EDIT: Fixing the switch's width, setting singleLine="true" and ellipsize="end" does not have any effect. 编辑:固定开关的宽度,设置singleLine =“ true”和ellipsize =“ end”无效。

You've written you tried singleLine="false" but this only sets it to be not one-lined. 您已经编写了尝试singleLine="false"但这仅将其设置为不singleLine="false" If you'd like to ellipsize, you should set: 如果您想放大,请设置:

android:ellipsize="end"
android:singleLine="true"

Also play with Switch's layout_width : now it fills parent, try to set the exact size you need, if there is. 也可以使用Switch的layout_width :现在填充父对象,尝试设置所需的确切大小(如果有)。 This way the widget will always the size you want, and the text will ellipsize. 这样,小部件将始终保持所需的大小,文本将变为椭圆大小。

If you need to ellipsize, use singleLine=true then ellipsize=end. 如果需要椭圆尺寸,请使用singleLine = true,然后使用ellipsize = end。 Your custom view should have a fixed width (either by weight or fixed dp). 您的自定义视图应具有固定的宽度(按重量或固定的dp)。 If you want the text resized, there's more customization needed. 如果要调整文本大小,则需要更多自定义。 Check this out: https://stackoverflow.com/a/17782522 看看这个: https : //stackoverflow.com/a/17782522

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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