【问题标题】:Refrencing a selector drawable from a style results blank imagebutton从样式中引用可绘制的选择器会导致空白图像按钮
【发布时间】: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


【解决方案1】:

这里,你正在为comment_actions布局设置样式,它是一个相对布局,它无法响应你的“android:src”属性。

<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" /> <!--here,you are setting a style for the RelativeLayout -->
</LinearLayout>

【讨论】:

  • 我知道几个小时前,现在我在代码中为这些图像按钮(如、不喜欢等)设置了可绘制对象,尽管仍然存在一个问题,即当我引用选择器时,什么都没有绘制在按钮上,但是当引用 png 可绘制对象时,按钮会获取该 png。
  • 如何引用选择器?
  • 我在drawable文件夹中有一个like.xml,并以这种方式引用:an_ImageButton.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), R.drawable.like));
  • 如何使用 button.setImageResource(resId);
  • 我会尝试,但是如果我使用 imageView 而不是 imagebutton 会怎样
猜你喜欢
  • 1970-01-01
  • 2018-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-05
  • 2012-11-26
相关资源
最近更新 更多