ImageButton选择器不起作用。

5
我将翻译以下内容:

我正在尝试为ImageView设置选择器,但它无法正常工作

我的布局

 <LinearLayout
        android:id="@+id/actionLayout"
        android:orientation="horizontal"
        android:background="@color/orange_color"
        android:gravity="center_vertical"
        android:paddingTop="@dimen/actions_top_bottom"
        android:paddingBottom="@dimen/actions_top_bottom"

        android:focusable="false"
        android:focusableInTouchMode="false"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="@dimen/actions_height"
            android:id="@+id/btnNoRecord"
            android:scaleType="centerInside"
            android:src="@drawable/ic_action_record"
            android:onClick="onRecordSwitcherClick"
            android:layout_weight="1"
            android:background="@drawable/selector"/>
...
</LinearLayout>

我的selector.xml文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="@color/dark_orange_color"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/orange_color"/>
        </shape>
    </item>
</selector>

bug在哪里?也许LinearLayout属性中有一些错误?

编辑 我刚刚发现ImageButton位于LinearLayout之外可以正常工作。但我真的需要layout。


选择器的名称是selector还是Myselector.xml? - Hassaan Rabbani
编号为 selector.xml。 - sagus_helgy
你的选择器在我的设备上运行正常。你遇到了什么问题? - Manishika
当我按下按钮时,我没有看到背景改变。 - sagus_helgy
你想要阴影吗?@Sufferer - SweetWisher ツ
@Rani 不,我不想要阴影 - 只要背景。 - sagus_helgy
6个回答

3

如下所示,在您的ImageButton中设置clickable属性:

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="@dimen/actions_height"
    android:id="@+id/btnNoRecord"
    android:src="@drawable/ic_action_record"
    android:background="@drawable/selector"
    android:clickable="true"/>

ImageButton 是从 ImageView 扩展而来的。默认情况下,clickable 属性为 false,因此您需要手动设置它。


ImageButton是从ImageView扩展而来的,clickable属性默认为false。 - staky
不要忘记添加选择器,没有选择器就无法工作!!! - Fortran

3

在你的ImageView中添加padding以查看背景选择器

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="@dimen/actions_height"
        android:padding="5dp"
        android:id="@+id/btnNoRecord"
        android:scaleType="centerInside"
        android:src="@drawable/ic_action_record"
        android:onClick="onRecordSwitcherClick"
        android:layout_weight="1"
        android:background="@drawable/selector"/>

                         OR 

尝试像这样操作,这会起作用,您需要添加填充以使背景选择器可见。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:state_pressed="true">
       <shape android:shape="rectangle">
            <solid android:color="@color/dark_orange_color"/>
            <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
      </shape>
</item>
<item>
      <shape android:shape="rectangle">
          <solid android:color="@color/orange_color"/>
          <padding
            android:bottom="5dp"
            android:left="5dp"
            android:right="5dp"
            android:top="5dp" />
      </shape>
</item>
</selector>

在结束文件中忘记了</selector>。 - Fortran

2

您的选择器看起来不太正确。

我认为您可能遗漏了状态(例如 state_focused="true" 等)。请记住,选择器是从上到下按顺序分析的(因此如果首先遇到一个状态,则选择器中的其他项目将被忽略 - 即顺序很重要)。

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@drawable/btn_settings_inset_top_pressed"
        android:state_focused="true"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top_pressed"
        android:state_focused="false"
        android:state_pressed="true"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top"
        android:state_focused="true"
        android:state_pressed="false"/>
    <item
        android:drawable="@drawable/btn_settings_inset_top"
        android:state_focused="false"
        android:state_pressed="false"/>
</selector>

我不需要关注焦点状态。同时,<item android:state_pressed="true" android:state_focused="true">...</item> 也不起作用。 - sagus_helgy
这是我使用的选择器,肯定是可用的。如果您不需要所有特定状态,则可以省略您不需要的状态。 - Booger
1
谢谢。我已经尝试了,但它不起作用。你试过将选择器作为ImageButton的android:background属性吗? - sagus_helgy
是的,我现在正在使用这段精确的代码,通过将此选择器设置为背景来实现。它在生产环境中工作。 - Booger

1
我已经解决了它!
在我的代码中,我有:
btnNoRecord.setOnTouchListener(new View.OnTouchListener() {
   @Override
   public boolean onTouch(View view, MotionEvent motionEvent)
   {
       ...
       return true;
   }
});

我把 return true 改成了 return false,现在它可以正常工作了!

0
在这种情况下,只需交换属性:
android:src
android:background

android:src="@drawable/selector"
android:background="@drawable/ic_action_record"

提示:您可以通过以下方式删除背景:

android:background="@null"

0

我知道现在已经很晚了,但这仍然需要一些解释。 在StackOverflow上有很多这样的问题,但没有人真正解释为什么会发生这种情况。请记住,每当您将ScaleType应用于视图时,除非您给它一些填充,否则其选择器可绘制部分将不会显示出来。实际上发生的是,它取出了所有视图的宽度和高度。选择器确实起作用,但src、drawable会重叠在其上。尤其是在FitXY的情况下更容易发生。尝试添加一些填充,它就可以工作了。以后再感谢我吧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接