【发布时间】:2013-07-19 00:47:30
【问题描述】:
我想要一个 ImageButtons 根据其状态更改图像。 (与九个 gag 和 facebook 应用程序中的“喜欢”按钮相同)。
我为它们中的每一个定义了一个样式,将选择器引用为可绘制对象,但是当我运行程序时,图像不会出现。你能告诉我这种方法有什么问题吗? (对不起我的英语不好)
values 文件夹中的comment_actions_style:
<style name="LikeStyle">
<item name="android:src">@drawable/like</item>
</style>
<style name="DislikeStyle">
<item name="android:src">@drawable/dislike_normal</item>
</style>
<style name="ReplyStyle">
<item name="android:src">@drawable/reply_normal</item>
</style>
<style name="ShareStyle">
<item name="android:src">@drawable/share</item>
</style>
</resources>
和drawable文件夹中的drawable一样:(其他按钮同理)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/like_clicked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="@drawable/like_focused" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="@drawable/like_normal"/>
<!-- default -->
</selector>
编辑:
我忘了提,我有一个列表视图,它的项目是用户 cmets,并且 cmets 有上面描述的按钮。
这是我的项目布局(部分)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/comment_content"
android:orientation="horizontal"
>
<include
android:id="@+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="@layout/comment_actions"
style="@style/LikeStyle" />
</LinearLayout>
和comment_actions 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/comment_action"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip">
<ImageButton
android:id="@+id/comment_action_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@android:color/transparent"
/>
<TextView
android:id="@+id/comment_action_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/comment_action_img"
android:gravity="center"
android:layout_marginLeft="5dip"/>
</RelativeLayout>
如果有必要,这些按钮也会从布局继承。
【问题讨论】:
-
你如何使用这些样式?你能发布一些代码吗?
-
@kevinhoo 我已经编辑了代码。如果您需要更多信息,请联系this问题
-
什么是comment_actions?你能展示一下吗?
-
@kevinhoo 我有 4 个布局相同的按钮,称为 comment_actions。这是最后的代码部分。在另一个 xml 文件中,我定义了这 4 个按钮,它们继承自 comment_actions 布局(第三代码部分)。我希望我已经理解了你的问题。
标签: android xml styles imagebutton