如何根据Android中的按钮单击动态更改图像视图边距

时间:2013-07-31 07:24:39

标签: android android-emulator

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

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));
        }
    });
}
}

2 个答案:

答案 0 :(得分: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);
    }
});

答案 1 :(得分: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);
}