今天在使用.9.png和setBackgroundResource修改按钮背景的时候,发现了一个奇怪的现像,界面离奇消失了,不知道是为什么,但是当我把按钮的背景资源修改为使用其它资源时,就正常了只要一点击“测试按钮”,右边的那部分ui就会消失按钮测试资源如下:
按钮正常:
按钮按下:测试代码如下:
public class MainActivity extends Activity
{ private RelativeLayout baseLayout;
private MyHorizontalScrollView svTop;
private RelativeLayout topLayout;
private boolean isScorllL = false;
private boolean isScorllR = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenW = metrics.widthPixels;
int screenH = metrics.heightPixels;
baseLayout = new RelativeLayout(this);
topLayout = new RelativeLayout(this);
svTop = new MyHorizontalScrollView(this);
svTop.setHorizontalScrollBarEnabled(false);
ImageView imgTop = new ImageView(this);
imgTop.setId(30003);
imgTop.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams imgtopParams = new RelativeLayout.LayoutParams(screenW, LayoutParams.MATCH_PARENT);
imgtopParams.leftMargin = 200;
topLayout.addView(imgTop, imgtopParams);
ImageView imgTopRight = new ImageView(this);
imgTopRight.setId(30002);
imgTopRight.setBackgroundColor(Color.CYAN);
RelativeLayout.LayoutParams imgtoprightParams = new RelativeLayout.LayoutParams(200, LayoutParams.MATCH_PARENT);
imgtoprightParams.addRule(RelativeLayout.RIGHT_OF, imgTop.getId());
ImageView imgTopLeft = new ImageView(this);
imgTopLeft.setBackgroundColor(Color.CYAN);
RelativeLayout.LayoutParams imgtopleftParams = new RelativeLayout.LayoutParams(200, LayoutParams.MATCH_PARENT);
imgtopleftParams.leftMargin = -200;
imgtopleftParams.addRule(RelativeLayout.LEFT_OF, imgTop.getId());
ButtonEx btn2 = new ButtonEx(this);
btn2.setText("测试按钮");
RelativeLayout.LayoutParams btn2Params = new RelativeLayout.LayoutParams(150, 80);
btn2Params.addRule(RelativeLayout.RIGHT_OF, imgTop.getId());
topLayout.addView(imgTopRight, imgtoprightParams);
topLayout.addView(imgTopLeft, imgtopleftParams);
topLayout.addView(btn2, btn2Params);
ButtonEx btn = new ButtonEx(this);
btn.setId(30001);
btn.setText("右滚动");
btn.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
if (isScorllL)
{
svTop.smoothScrollTo(200, 0);
isScorllL = false;
}
else
{
svTop.smoothScrollTo(400, 0);
isScorllL = true;
}
}
}
);
RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(100, 70);
btnParams.leftMargin = screenW-100+200;
btnParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
topLayout.addView(btn, btnParams);
svTop.addView(topLayout);
baseLayout.addView(svTop);
ViewTreeObserver vto = svTop.getViewTreeObserver();
vto.addOnGlobalLayoutListener
(
new ViewTreeObserver.OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
svTop.scrollTo(200, 0);
}
}
);
this.setContentView(baseLayout);
}
public class MyHorizontalScrollView extends HorizontalScrollView
{ public MyHorizontalScrollView(Context context) {
super(context);
}
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
public class ButtonEx extends Button{ private boolean auto = true;
private boolean hasNorBg = false;
private boolean hasPressBg = false;
private int norBg = R.drawable.left_bar_btn_nor;
private int pressBg = R.drawable.left_bar_btn_press;
public ButtonEx(Context context) {
super(context);
this.setBackgroundResource(norBg);
this.setTextColor(Color.WHITE);
this.setOnTouchListener
(
new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (auto)
{
if (event.getAction()==MotionEvent.ACTION_UP)
{
setBackgroundResource(norBg);
}
else if (event.getAction()==MotionEvent.ACTION_DOWN)
{
setBackgroundResource(pressBg);
}
}
return false;
}
}
);
}
public void setNorBg(int res)
{
norBg = res;
hasNorBg = true;
setBackgroundResource(norBg);
}
public void setPressBg(int res)
{
pressBg = res;
hasPressBg = true;
}
public void autoChange(boolean b)
{
auto = b;
}
}
}
按钮正常:
按钮按下:测试代码如下:
public class MainActivity extends Activity
{ private RelativeLayout baseLayout;
private MyHorizontalScrollView svTop;
private RelativeLayout topLayout;
private boolean isScorllL = false;
private boolean isScorllR = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenW = metrics.widthPixels;
int screenH = metrics.heightPixels;
baseLayout = new RelativeLayout(this);
topLayout = new RelativeLayout(this);
svTop = new MyHorizontalScrollView(this);
svTop.setHorizontalScrollBarEnabled(false);
ImageView imgTop = new ImageView(this);
imgTop.setId(30003);
imgTop.setBackgroundColor(Color.GRAY);
RelativeLayout.LayoutParams imgtopParams = new RelativeLayout.LayoutParams(screenW, LayoutParams.MATCH_PARENT);
imgtopParams.leftMargin = 200;
topLayout.addView(imgTop, imgtopParams);
ImageView imgTopRight = new ImageView(this);
imgTopRight.setId(30002);
imgTopRight.setBackgroundColor(Color.CYAN);
RelativeLayout.LayoutParams imgtoprightParams = new RelativeLayout.LayoutParams(200, LayoutParams.MATCH_PARENT);
imgtoprightParams.addRule(RelativeLayout.RIGHT_OF, imgTop.getId());
ImageView imgTopLeft = new ImageView(this);
imgTopLeft.setBackgroundColor(Color.CYAN);
RelativeLayout.LayoutParams imgtopleftParams = new RelativeLayout.LayoutParams(200, LayoutParams.MATCH_PARENT);
imgtopleftParams.leftMargin = -200;
imgtopleftParams.addRule(RelativeLayout.LEFT_OF, imgTop.getId());
ButtonEx btn2 = new ButtonEx(this);
btn2.setText("测试按钮");
RelativeLayout.LayoutParams btn2Params = new RelativeLayout.LayoutParams(150, 80);
btn2Params.addRule(RelativeLayout.RIGHT_OF, imgTop.getId());
topLayout.addView(imgTopRight, imgtoprightParams);
topLayout.addView(imgTopLeft, imgtopleftParams);
topLayout.addView(btn2, btn2Params);
ButtonEx btn = new ButtonEx(this);
btn.setId(30001);
btn.setText("右滚动");
btn.setOnClickListener
(
new Button.OnClickListener()
{
public void onClick(View v)
{
if (isScorllL)
{
svTop.smoothScrollTo(200, 0);
isScorllL = false;
}
else
{
svTop.smoothScrollTo(400, 0);
isScorllL = true;
}
}
}
);
RelativeLayout.LayoutParams btnParams = new RelativeLayout.LayoutParams(100, 70);
btnParams.leftMargin = screenW-100+200;
btnParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
topLayout.addView(btn, btnParams);
svTop.addView(topLayout);
baseLayout.addView(svTop);
ViewTreeObserver vto = svTop.getViewTreeObserver();
vto.addOnGlobalLayoutListener
(
new ViewTreeObserver.OnGlobalLayoutListener()
{
public void onGlobalLayout()
{
svTop.scrollTo(200, 0);
}
}
);
this.setContentView(baseLayout);
}
public class MyHorizontalScrollView extends HorizontalScrollView
{ public MyHorizontalScrollView(Context context) {
super(context);
}
public boolean onTouchEvent(MotionEvent ev) {
return false;
}
}
public class ButtonEx extends Button{ private boolean auto = true;
private boolean hasNorBg = false;
private boolean hasPressBg = false;
private int norBg = R.drawable.left_bar_btn_nor;
private int pressBg = R.drawable.left_bar_btn_press;
public ButtonEx(Context context) {
super(context);
this.setBackgroundResource(norBg);
this.setTextColor(Color.WHITE);
this.setOnTouchListener
(
new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (auto)
{
if (event.getAction()==MotionEvent.ACTION_UP)
{
setBackgroundResource(norBg);
}
else if (event.getAction()==MotionEvent.ACTION_DOWN)
{
setBackgroundResource(pressBg);
}
}
return false;
}
}
);
}
public void setNorBg(int res)
{
norBg = res;
hasNorBg = true;
setBackgroundResource(norBg);
}
public void setPressBg(int res)
{
pressBg = res;
hasPressBg = true;
}
public void autoChange(boolean b)
{
auto = b;
}
}
}
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/base_btn_clicked"/>
<item android:state_focused="true" android:drawable="@drawable/base_btn_clicked"/>
<item android:drawable="@drawable/base_btn"/>
</selector>
这可以有按下去,没按的 效果。放到drawable目录下。
Button btn;
btn.setBackground(getResources().getDrawable(R.drawable.xxx));
感谢回答,我试下但是为什么我上面的代码设置的按钮就不能使用呢?
应该不是.9.png做的有问题,我只要把这个按钮不放在滑动出的右边框,就可以正常显示
要不你把代码改写一下,这样看着太复杂,太累。
这个代码已经是从现在做的项目中剥离出来的简化版测试代码了
由于这个bug必须是把按钮放在右边滑动出来的侧栏中才会出现
所以我发的代码中还包括滑动出来侧栏这个功能,所以可能多一些
相关的工程在这里可以下载:
http://download.csdn.net/detail/inzaghi97/6384305
我试了下,模拟器可以正常显示,但是一放到真机上,程序就崩溃了,这是为什么呢?
手机是moto mb525
这是测试的代码<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true" android:drawable="@drawable/btn_press"/>
<item android:drawable="@drawable/btn_nor"/>
</selector>package com.example.testradiobutton;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Button;
import android.widget.RelativeLayout;public class MainActivity extends Activity
{ private RelativeLayout baseLayout;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
baseLayout = new RelativeLayout(this);
Button btn2 = new Button(this);
btn2.setText("测试按钮");
btn2.setBackground(this.getResources().getDrawable(R.drawable.test_btn));
baseLayout.addView(btn2); this.setContentView(baseLayout);
}
}
在真机上测试没有成功,我再试试其它的机器