【问题标题】:StateListDrawable not working on TableRowStateListDrawable 在 TableRow 上不起作用
【发布时间】:2011-10-23 01:41:59
【问题描述】:

我正在尝试在 TableLayout 中获取 TableRow,以在用户触摸它时更改背景颜色,但它似乎不起作用。当我单击 TableRow 时,没有任何反应。这是我目前所拥有的:

布局:

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  style="@style/BaseStyle"
   android:stretchColumns="*">

      <TableRow style="@style/TableRow">   
           <TextView 
                 android:id="@+id/settings
                 style="@style/BaseStyle.SettingsText"
                 android:text="Settings"
                 android:onClick="onClick"
                 android:clickable="true"
            />  
      </TableRow>

 </TableLayout>

风格:

<style name="TableRow">
     <item name="android:background">@drawable/onclickhighlight</item>
</style>

可绘制(onclickhighlight.xml)

<selector xmlns:android="http://schemas.android.com/apk/res/android">

     <!--  Active state --> 
     <item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/background_light" />

      <!--  Inactive state-->
     <item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="@android:color/transparent" />

      <!--  Pressed state-->
      <item android:state_selected="true" android:state_focused="true" android:state_pressed="true" android:drawable="@android:color/background_light" />

</selector>

颜色:

 <resources>
     <color name="background_light">#FFFFFF</color>
 </resources>

我知道 TableRow 正在使用 onclickhighlight StateListDrawable,因为如果我将“非活动状态”的 android:drawable 属性从 @android:color/transparent 更改为 @android:color/background_light,背景颜色会变为白色。

【问题讨论】:

    标签: android android-layout statelist


    【解决方案1】:

    我遇到了完全相同的问题,我正在努力寻找答案。我敢打赌,如果您要从表格行 xml 中删除 android:onClick="onClick",您会发现突出显示会起作用。但是,您如何让点击事件与您想要的方法相关联?!

    【讨论】:

    • 很高兴知道我不是唯一一个。我将 onClick 从 TextView 移动到 TableRow,因为我希望整行可点击,但仍然没有突出显示。也许是一个错误?
    【解决方案2】:

    我遇到了同样的问题,这对我有用。

    <?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android">   
        <item android:state_pressed="true" android:drawable="@color/yourcolor" />
        <item android:drawable="@color/yournormalbackground"/> 
    </selector>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      相关资源
      最近更新 更多