根据服务器响应动态更改android中的按钮位置

时间:2016-01-06 15:35:56

标签: android relativelayout

从服务器我获得一个支付类型列表,如CreditCardPayment,PayPalCheckout,GoogleWallet。根据从服务器列表中发送的顺序,我在屏幕上按钮的顺序会发生变化。有人可以指出如何在屏幕上动态更改按钮的位置吗?

1 个答案:

答案 0 :(得分:0)

您可以使用相对布局。并在代码中使用RelativeLayout.BELOW之类的属性来根据需要动态更改位置。 请检查How to set RelativeLayout layout params in code not in xml链接

您需要动态创建布局,如下所示

public class MainActivity extends AppCompatActivity {
    Boolean button1ontop;
    Boolean button2ontop;
    Boolean button3ontop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        button1ontop=false;
        button2ontop=true;
        button3ontop=false;
        //create new layout and set height and width
        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams= new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        //add buttons
        Button b1 = new Button(this);
        Button b2 = new Button(this);
        Button b3 = new Button(this);
        b1.setText("Button1");
        b1.setId(R.id.button1);
        b2.setId(R.id.button2);
        b3.setId(R.id.button3);
        b2.setText("Button2");
        b3.setText("Button3");
       // Toast.makeText(this,(b1.getId()),Toast.LENGTH_SHORT).show();
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        if(button1ontop){
            b1.setLayoutParams(lp);
            lp1.addRule(RelativeLayout.BELOW, R.id.button1);
            b2.setLayoutParams(lp1);
            lp2.addRule(RelativeLayout.BELOW,R.id.button2);
            b3.setLayoutParams(lp2);
        }
        else if(button2ontop){
            b2.setLayoutParams(lp);
            lp1.addRule(RelativeLayout.BELOW, R.id.button2);
            b1.setLayoutParams(lp1);
            lp2.addRule(RelativeLayout.BELOW,R.id.button1);
            b3.setLayoutParams(lp2);
        }
        else if(button3ontop){
            b3.setLayoutParams(lp);
            lp1.addRule(RelativeLayout.BELOW, R.id.button3);
            b1.setLayoutParams(lp1);
            lp2.addRule(RelativeLayout.BELOW,R.id.button1);
            b2.setLayoutParams(lp2);
        }
        relativeLayout.addView(b1);
        relativeLayout.addView(b2);
        relativeLayout.addView(b3);

        setContentView(relativeLayout, layoutParams);

    }



}

res / values文件夹中的Crete文件ids.xml并定义ids

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="button1" />
    <item type="id" name="button2" />
    <item type="id" name="button3" />

</resources>

根据您的要求修改此代码