【问题标题】:Unable to show a Border/Stroke when clicking on an ImageButton单击 ImageButton 时无法显示边框/描边
【发布时间】:2020-06-09 13:47:45
【问题描述】:

我正在使用ImageButtons,但单击它们时无法创建边框。我知道关于这个主题还有另一个question,但是在按照答案之后,我无法让它发挥作用。

目前我的代码是:

ib_shape_corner.xlm

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
        android:width="3dp"
        android:color="@color/blue1" />
    <padding
        android:left="3dp"
        android:top="3dp"
        android:right="3dp"
        android:bottom="3dp" />
    <corners
        android:bottomRightRadius="8dip"
        android:bottomLeftRadius="8dip"
        android:topRightRadius="8dip"
        android:topLeftRadius="8dip" />
</shape>

ib_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/ib_shape_corner" />
    <item android:drawable="@android:color/transparent" />
</selector>

ma​​in.xml

 <ImageButton
            android:id="@+id/ib_img"
            android:layout_width="160dp"
            android:layout_height="100dp"
            android:background="@drawable/ib_selector"
            android:src="@drawable/ic_img"
            android:padding="5dp">

ma​​in.java

在这里我尝试设置为按下并在单击按钮时选择我的按钮,但没有成功

    ib_img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ib_img.setSelected(true);
            ib_img.setPressed(true);
           // Do my stuff

        }
    });

listener 有效,因为它在单击时实际运行我的代码,但未添加边框。

【问题讨论】:

    标签: android border imagebutton


    【解决方案1】:

    尝试使用:

    android:focusable="true"
    android:clickable="true"
    

    为您的 ImageButton。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多