Android ImageButton交换资源会留下“残留”

时间:2016-09-24 17:32:22

标签: java android imagebutton

我有2张图片:

IMG_1:

enter image description here

IMG_1_PRESSED:

enter image description here

我默认显示IMG_1,但是当我点击它时,图像应该更改为IMG_1_PRESSED。

以下是按钮本身的代码段,其中be是包含正确drawables的enum

    ImageButton ib = new ImageButton(this.context);

    ib.setImageResource(be.getDrawable());
    ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
    ib.setBackgroundColor(Color.TRANSPARENT);
    ib.setPadding(0, 0, 0, 0);

    ib.setAdjustViewBounds(true);
    ib.setMaxWidth(width);
    ib.setMaxHeight(height);

    setStrengthListener(ib, be);

这是setStrengthListener方法:

private void setStrengthListener(final ImageButton ib, final ButtonEnum be){
    ib.setOnTouchListener(new View.OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    ib.setImageResource(be.getDrawablePressed());
                    return true;
                case MotionEvent.ACTION_UP:
                    ib.setImageResource(be.getDrawable());
                    return true;
            }
            return false;
        }
    });
}

现在两个图像都是480x240,widthheight分别设置为设备宽度/ 3和设备宽度/ 6(我想显示3个图像,因此/ 3)。

这就是问题,如果按下按钮,我明白了:

enter image description here

在按下的按钮下面有一条小线......我已经尝试了很多来解决这个问题,但不知何故,这条小线已经成为我今天最大的敌人。

作为额外的努力:如果我将原始资源设置为IMG_1_PRESSED,则该行不再显示:

    ImageButton ib = new ImageButton(this.context);

    ib.setImageResource(be.getDrawablePressed());
    ib.setScaleType(ImageButton.ScaleType.CENTER_INSIDE);
    ib.setBackgroundColor(Color.TRANSPARENT);
    ib.setPadding(0, 0, 0, 0);
    ....etc

但显然我不想以按下的按钮开始。

亲爱的Android专家,我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

这是我所知道的最简单的解决方案, 试试这个

Drawable 文件夹中创建Xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_pressed_img"android:state_pressed="true" />
<item android:drawable="@drawable/btn_normal_image" />
</selector>

然后将此 drawable 文件添加为View

的背景

示例:

<ImageView
    android:id="@+id/enter_btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:maxHeight="100dip"
    android:maxWidth="100dip"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:background="@drawable/enter_btn" />

或Java代码

yourView.setImageResource(R.drawable.*drawablefile*);

您也可以使用ImageButton,但ImageView看起来比ImageButton好,因为它没有按钮边框。

答案 1 :(得分:0)

经过大量的反复尝试后,我设法将灰色线条拿走了。最后它真的很简单,虽然我不完全确定为什么要修复它:

主要布局

    LinearLayout ll = new LinearLayout(this);
    ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
    ll.setOrientation(LinearLayout.HORIZONTAL);
    ll.setGravity(Gravity.TOP);

视图创建(imageview的布局未更改):

ImageView iv = tbb.getTopBarButton(ButtonEnum.IMG_1);

LinearLayout iViewLayout = new LinearLayout(this);
iViewLayout.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
iViewLayout.setOrientation(LinearLayout.HORIZONTAL);

iViewLayout.addView(iv);

ll.addView(iViewLayout);

每个imageview添加一个单独的布局似乎删除了灰线。很想知道为什么这个有用,但为了将来的参考,至少我希望能帮助别人。