2016-01-06 58 views
2

從服務器獲取CreditCardPayment,PayPalCheckout,GoogleWallet等付款類型列表。根據這些從服務器發送到列表中的順序,我在屏幕上按鈕的順序將會改變。有人能指出如何在屏幕上動態改變按鈕的位置嗎?根據服務器響應動態更改android中的按鈕位置

+0

你想爲它設置X和Y嗎? –

+0

nope它會像按鈕w按鈕x按鈕y按鈕z – alibaba

+0

爲什麼不動態設置'按鈕'文本代替改變按鈕的位置? – NightWatcher

回答

0

您可以使用相對佈局。並在您的代碼中使用像RelativeLayout.BELOW這樣的屬性,以按照需求動態更改位置。 請檢查How to set RelativeLayout layout params in code not in xml鏈接

您需要在res動態創建佈局如下

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

    } 



} 

克里特文件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> 

修改該代碼,根據您的要求

+0

在我的佈局中,我添加了3個按鈕佈局。顯示這些的順序我想要在代碼中動態更改 – alibaba

+0

我的佈局中有一個用於谷歌錢包的框架佈局。我不能在我的代碼中動態聲明它。我只能在代碼中得到一個引用,然後動態改變它的位置,但不知道我該怎麼做 – alibaba