0

我正在尝试根据按钮单击事件更改 imageview 位置,我尝试使用下面的代码,如果单击 button1 ic_lancher 图像显示一个位置,然后再次单击 button2 在这里我只是更改了左边距,但图像没有移动,但在 button2点击时如果给出不同的背景意味着时间它正在完美移动,如果我在点击时在两个按钮中给出相同的背景,只是改变图像的位置意味着它不能正常工作,首先如果我点击另一个按钮,图像显示在哪里,图像也没有移动,它存在于相同的位置。如何解决这个问题?

public class MainActivity extends Activity {
Button b1,b2;
ImageView arrow;
float screenHeight,screenWidth,screendensity;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
         screenHeight = displaymetrics.heightPixels;
         screenWidth = displaymetrics.widthPixels;
    setContentView(R.layout.activity_main);

    b1=(Button)findViewById(R.id.button1);
    b1.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
              arrow=(ImageView)findViewById(R.id.imageView1);
              arrow.setBackgroundResource(R.drawable.ic_launcher);
              arrow.setVisibility(View.VISIBLE);
              RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
              layoutarrow.leftMargin= (int)(120*(screenWidth/1024));
              layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
              layoutarrow.width= (int)(50*(screenWidth/1024));
              layoutarrow.height= (int)(20*(screenHeight/600));
        }
    });

    b2=(Button)findViewById(R.id.button2);
    b2.setOnClickListener(new OnClickListener() {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub


            arrow=(ImageView)findViewById(R.id.imageView1);
              arrow.setBackgroundResource(R.drawable.ic_launcher);
              arrow.setVisibility(View.VISIBLE);
              RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
              layoutarrow.leftMargin= (int)(500*(screenWidth/1024));
              layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
              layoutarrow.width= (int)(50*(screenWidth/1024));
              layoutarrow.height= (int)(20*(screenHeight/600));
        }
    });
}
}
4

2 回答 2

1

试试这个-

b2.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub


        arrow=(ImageView)findViewById(R.id.imageView1);
          arrow.setBackgroundResource(R.drawable.ic_launcher);
          arrow.setVisibility(View.VISIBLE);


          RelativeLayout.LayoutParams layoutarrow = 
                            new RelativeLayout.LayoutParams((int)(50*(screenWidth/1024)),(int)(20*(screenHeight/600));
                    layoutarrow.setMargins((int)(500*(screenWidth/1024)), 0, 0,(int)(235*(screenHeight/600)));

                    arrow.setLayoutParams(layoutarrow);
    }
});
于 2013-07-31T07:36:07.653 回答
0

您需要在这两种onClick方法的代码下方添加行。

arrow.setLayoutParams(layoutarrow);

这就像

public void onClick(View arg0) {
     // TODO Auto-generated method stub
    arrow=(ImageView)findViewById(R.id.imageView1);
    arrow.setBackgroundResource(R.drawable.ic_launcher);
    arrow.setVisibility(View.VISIBLE);
    RelativeLayout.LayoutParams layoutarrow = (RelativeLayout.LayoutParams)arrow.getLayoutParams();    
    layoutarrow.leftMargin= (int)(120*(screenWidth/1024));
    layoutarrow.bottomMargin= (int)(235*(screenHeight/600));
    layoutarrow.width= (int)(50*(screenWidth/1024));
    layoutarrow.height= (int)(20*(screenHeight/600));
    arrow.setLayoutParams(layoutarrow);
}
于 2013-07-31T07:37:34.503 回答