2015-07-03 27 views
0

我在线性布局中使用样式来创建带文本的可点击的圆圈 - 代表一周中的某一天。虽然点击功能无法正常工作,但我正在将形状设计得很好。可点击的圆和线性布局中的文本?

这里是我的线性布局

<LinearLayout 
     android:layout_height="40dp" 
     android:layout_width="40dp" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:clickable="true" 
     android:focusable="true" 
     style="@style/circleButton_style" 

     android:saveEnabled="true" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="#FFFFFF" 
      android:textSize="10sp" 
      android:textStyle="bold" 
      android:singleLine="true" 
      android:text="S"/> 

    </LinearLayout> 

风格

<style name="circleButton_style" parent="AppTheme"> 
    <item name="android:background">@drawable/circle_stand_sel</item> 
    <item name="android:textColor">#FFFFFF</item> 
    <item name="android:minHeight">48dp</item> 
    <item name="android:paddingLeft">5dp</item> 
    <item name="android:paddingRight">5dp</item> 
</style> 

而且我绘制

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

<!-- initial state state --> 
<item android:drawable="@drawable/circle" android:state_first="false" /> 

<!-- disabled state --> 
<item android:drawable="@drawable/circle" android:state_enabled="false"/> 

<!-- enabled and pressed state --> 
<item android:drawable="@drawable/circle_pressed" android:state_enabled="true" android:state_pressed="true"/> 

<!-- enabled and focused state --> 
<item android:drawable="@drawable/circle_pressed" android:state_enabled="true" android:state_focused="true"/> 

<!-- enabled state --> 
<item android:drawable="@drawable/circle_pressed" android:state_enabled="true"/> 

它根本没有移动circle_pressed状态,我不知道为什么......任何人都可以帮忙?

+0

您是否有意在drawable xml上留下了一些代码,或者是该文件的内容?即关闭选择器标签等。 – Broak

+0

是啊,我的坏。代码的功能,虽然它只是给我展示了“圈子”可绘制,并且不允许任何点击功能 –

+0

噢好的很酷,你有没有尝试添加clickable到textview呢?我在这里看不到任何明显的错误! – Broak

回答

0

state_first,state_middlestate_last用于为某个ViewGroups中的第一个,中间或最后一个项目创建不同的样式。没有太多的文档,所以我不知道他们在哪里使用。

按顺序评估选择器中的每个项目,并应用匹配的第一个项目。所以,基本上,你的第一个项目总是匹配。您的默认状态应该是列表中的最后一项:

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/circle_pressed" 
      android:state_pressed="true"/> 
    <item android:drawable="@drawable/circle_pressed" 
      android:state_focused="true"/> 
    <item android:drawable="@drawable/circle"/> 
</selector> 
+0

我猜这些选择器默认情况下不支持任何组件:https://sriramramani.wordpress.com/2014/03/13/pseudo-selectors/ – tachyonflux

+0

有了你提供的这个代码,我可以容纳这些圆圈,它们的颜色,虽然他们不会改变点击@karaokyo –

+0

我想我可以通过设置一个onClick方法来设置onEnabled –