【问题标题】:Android ToggleButton custom style not getting appliedAndroid ToggleButton 自定义样式未应用
【发布时间】:2013-01-22 01:15:45
【问题描述】:

我关注 this example 为我的切换按钮应用了一些自定义主题,但是当我运行我的应用程序时,我看到了通用的 android 切换图标 - 我想我在这里或那里缺少一个设置,可以使用一组额外的盯着它。

我有一个在 ListView 中使用的布局,我正在尝试将自定义可绘制对象用于选中/未选中状态,每个切换状态默认设置为 CHECKED:

<ToggleButton
        android:id="@+id/profile_item_value_text"
        style="@style/ProfileTagTheme"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:layout_gravity="center"
        android:gravity="center_vertical|center_horizontal"
        android:checked="true" />

这是我在styles 文件中为ProfileTagTheme 提供的内容:

<style name="Widget.Button.Toggle" parent="android:Widget">
    <item name="android:background">@drawable/profile_btn_toggle_bg</item>
    <item name="android:disabledAlpha">@android:attr/disabledAlpha</item>
</style>
<style name="ProfileTagTheme" parent="android:Theme.Black">
    <item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
</style>

反过来,profile_btn_toggle_bg.xml,住在我的主要drawable 目录中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@android:color/transparent"/>
    <item android:id="@android:id/toggle" android:drawable="@drawable/profile_btn_toggle"/>
</layer-list>

引用profile_btn_toggle,就在它旁边:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:drawable="@drawable/tag_active" /> <!-- pressed -->
    <item android:drawable="@drawable/tag" /> <!-- default/unchecked -->
</selector>

我已经验证了自定义图像存在于 drawables 目录中,所以显然要么我误解了样式级联方式的某些内容,要么我在这种疯狂的样式中遗漏了某处的参考。

【问题讨论】:

    标签: android android-listview android-button


    【解决方案1】:

    看来我唯一真正的问题是在我的ToggleButton 实现中缺少android:background 元素;所以对上面代码的唯一更改是添加该元素,例如

    <ToggleButton
            android:id="@+id/profile_item_value_text"
            style="@style/ProfileTagTheme"
            android:background="@drawable/profile_btn_toggle_bg"
            android:layout_width="0px"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:textSize="16dp"
            android:textColor="@color/black"
            android:layout_gravity="center"
            android:gravity="center_vertical|center_horizontal"
            android:checked="true"
            android:onClick="onToggleClicked" />
    

    【讨论】:

      【解决方案2】:

      我在我的一些应用程序中做了类似的事情。请记住,这是一种完全不同的样式方法,可能/可能不适用于您的情况。通过设置背景,我可以为切换按钮设置我想要的图像:

      <ToggleButton
          android:id="@+id/tb_useNotificationShortcuts"
          android:layout_width="100dp"
          android:layout_height="25dp"
          android:layout_gravity="center"
          android:background="@drawable/togglebutton"
          android:tag="21" />
      
      
      <?xml version="1.0" encoding="utf-8"?>
      <selector xmlns:android="http://schemas.android.com/apk/res/android">
      
         <item android:drawable="@drawable/custom_toggle_on" android:state_checked="true"/>
         <!-- pressed -->
         <item android:drawable="@drawable/custom_toggle_off"/>
         <!-- default/unchecked -->
      
      </selector>
      

      custom_toggle_on 和 custom_toggle_off 是我的图像,这些也可能是您的图层列表。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-01
        • 2014-08-23
        相关资源
        最近更新 更多