简体   繁体   English

Android-切换按钮上的文字叠加层图标

[英]Android - Text overlays icon on toggle button

预期结果

I have a toggle button with an icon and text as shown above (pointed at with the green arrow). 我有一个带图标和文本的切换按钮,如上图所示(用绿色箭头指向)。 Most devices I have tested worked just fine. 我测试过的大多数设备都可以正常工作。

Just with so far two Samsung tablets (Samsung Galaxy Tab 7.0 and a 10 inch Galaxy Tab) there is this problem where the text overlays the icon (pointed at with the red arrow) as shown below. 到目前为止,只有两台三星平板电脑(三星Galaxy Tab 7.0和10英寸Galaxy Tab)存在以下问题:文本覆盖图标(用红色箭头指向),如下所示。

样式错误

I have tried all aligments, paddings and such with no working result. 我已经尝试了所有设备,填充物等,但没有任何工作结果。

The web and SO surprisingly did not come up with a solution nor the problem. 令人惊讶的是,Web和SO都没有提出解决方案或问题。

This is the XML I am using right now for the toggle button. 这是我现在为切换按钮使用的XML。 It is wrapped amongst other defintions into a RelativeLayout: 它与其他定义一起包装到RelativeLayout中:

<ToggleButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/label_subscriptions"
    android:id="@+id/subscription_button"
    android:textOff="@string/btn_subscribe"
    android:textOn="@string/btn_subscribed"
    android:button="@drawable/star_selector"
    android:background="@android:color/transparent"
    android:textColor="@color/textColor"
    android:layout_toLeftOf="@+id/navigation_next_item"
    android:layout_alignBottom="@+id/channel_headline_image"
    />

The @drawable/star_selector has the following definition: @drawable/star_selector具有以下定义:

<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/ic_like" />
    <item android:state_checked="true" android:drawable="@drawable/ic_liked_fav" />
 </selector>

Thank you for any hints pointing me in the right direction! 感谢您为我指明正确方向的任何提示!

Actually its not working on lower APIs 实际上它不适用于较低的API

Change 更改

 android:button="@drawable/star_selector"

to

android:drawableLeft="@drawable/star_selector"

Hope it will help. 希望它会有所帮助。

To allow crossVersion properly you should use drawableStart instead of drawableLeft. 为了正确地允许crossVersion,您应该使用drawableStart而不是drawableLeft。 This issue appears only with Android 4.1 If you use drawableLeft you will not be able to add a padding between text and drawable, but with drawableStart you can use the "drawablePadding" property 仅在Android 4.1中会出现此问题。如果您使用drawableLeft,则将无法在文本和drawable之间添加填充,但是在drawableStart中,您可以使用“ drawablePadding”属性

Regards!!! 问候!!!

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

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