Monday, March 10, 2014

setBackgroundResource interface disappears cause problems


Today, the use of .9. Png and setBackgroundResource Modify button background, I found a strange phenomenon, the interface disappears mysteriously, do not know why, but when I put the button to change the background resource use other resources, it is normal the

Just a click on the "test button", that part of the ui on the right will disappear < br />
Button testing resources as follows:
Button Normal:
Button is pressed:

Test code is as follows:
 

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 ("test button");

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 ("scroll right");
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;
}
}
}


<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:






This can press down, not by the results. Put under drawable directory.
Button btn;
btn.setBackground (getResources () getDrawable (R.drawable.xxx).);
Reply:
to birdsaction:

cited a floor birdsaction reply:






This can press down, not by the results. Put under drawable directory.
Button btn;
btn.setBackground (getResources () getDrawable (R.drawable.xxx).);


Thanks for replying, I try

But why set my button code above can not be used?
Reply:
I looked at, is very strange, is not an ordinary picture, with a .9. Png will. There may be two png do have a problem, because the effect of pressing the wrong.
Reply:
Another proposed landlord to achieve the desired effect with android usual way, the code will be a lot clearer than it is now.
Reply:
reference to the third floor tantahe reply:
I looked at, is very strange, is not an ordinary picture, with a .9 png will. There may be two png do have a problem, because the effect of pressing the wrong.


Should not be .9. Png do have a problem, I just put this button does not slide out on the right side of the box, it can be displayed
Reply:
Click on the button when I see, onTouch and onClick came normal. do not know why.
Why do not you look at the code rewrite, so watching too complicated, too tired.
Reply:
reference to the 6th floor tantahe reply:
when I click the button, onTouch and onClick came normal. do not know why.
Why do not you look at the code rewrite, so watching too complicated, too tired.


This code has to do now is stripped from the project out of the simplified test code
Because of this bug must be put out of the slide button on the right sidebar appears
So I made the code also includes sidebar slide out this function, it may be more

Reply:
Find doubts ah!
Related engineering can be downloaded here:
http://download.csdn.net/detail/inzaghi97/6384305
Reply:
cited a floor birdsaction reply:






This can press down, not by the results. Put under drawable directory.
Button btn;
btn.setBackground (getResources () getDrawable (R.drawable.xxx).);


I tried, the simulator can be normal, but put on a real machine, the program crashes, and this is why?
Phone is moto mb525
Reply:
Is not memory overflow error if the image is too much may cause outofmemory error.
Reply:
reference to the 10th floor birdsaction reply:
is not a memory overflow error if the image is too much may cause outofmemory error
<. br />
This is the test code
 







 
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 ("test button");
btn2.setBackground (this.getResources () getDrawable (R.drawable.test_btn).);

baseLayout.addView (btn2);

this.setContentView (baseLayout);
}
}


The real test is not successful, I'll try other machines
Reply:
reference to the 10th floor birdsaction reply:
is not a memory overflow error if the image is too much may cause outofmemory error
<. br />
Can you tell me your contact information incorrect?
I want the project to you, help me see where wrong Okay?

No comments:

Post a Comment