Android ImageButton 交换资源离开 'residue'
Android ImageButton swap resource leaves 'residue'
我有 2 张图片:
IMG_1:
IMG_1_PRESSED:
我默认显示IMG_1,但是当我点击它时,图像应该变成IMG_1_PRESSED。
这是按钮本身的代码片段,其中 be
是包含正确可绘制对象的 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,width
和 height
分别设置为设备的 width/3 和设备的 width/6(我想显示 3 张图片,因此 / 3).
问题来了,如果按下按钮,我得到这个:
按下的按钮下方有一条小线...我已经尝试了很多方法来解决这个问题,但不知何故,这条小线已成为我今天最大的敌人。
额外的努力:如果我将原始资源设置为 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 专家,我做错了什么?
据我所知,这是最简单的解决方案,
试试这个
在您的 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
更好,因为它没有按钮边框。
经过大量的反复试验,我设法去掉了那条灰线。最后真的很简单,虽然我不完全确定为什么会这样:
主布局:
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);
为每个图像视图添加一个单独的布局似乎删除了灰线。很想知道为什么这有效,但为了将来参考,至少我希望能帮助别人。
我有 2 张图片:
IMG_1:
IMG_1_PRESSED:
我默认显示IMG_1,但是当我点击它时,图像应该变成IMG_1_PRESSED。
这是按钮本身的代码片段,其中 be
是包含正确可绘制对象的 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,width
和 height
分别设置为设备的 width/3 和设备的 width/6(我想显示 3 张图片,因此 / 3).
问题来了,如果按下按钮,我得到这个:
按下的按钮下方有一条小线...我已经尝试了很多方法来解决这个问题,但不知何故,这条小线已成为我今天最大的敌人。
额外的努力:如果我将原始资源设置为 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 专家,我做错了什么?
据我所知,这是最简单的解决方案, 试试这个
在您的 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
更好,因为它没有按钮边框。
经过大量的反复试验,我设法去掉了那条灰线。最后真的很简单,虽然我不完全确定为什么会这样:
主布局:
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);
为每个图像视图添加一个单独的布局似乎删除了灰线。很想知道为什么这有效,但为了将来参考,至少我希望能帮助别人。