從服務器獲取CreditCardPayment,PayPalCheckout,GoogleWallet等付款類型列表。根據這些從服務器發送到列表中的順序,我在屏幕上按鈕的順序將會改變。有人能指出如何在屏幕上動態改變按鈕的位置嗎?根據服務器響應動態更改android中的按鈕位置
2
A
回答
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>
修改該代碼,根據您的要求
相關問題
- 1. 根據按鈕的位置動態調整ImageView的位置
- 2. 在Android中更改按鈕的位置
- 3. 如何根據服務器響應更改PeriodicalExecuter的頻率?
- 4. 在服務器響應後更改按鈕圖像
- 5. 如何更改響應式設計中按鈕的位置?
- 6. Angularjs根據服務器響應更改複選框值
- 7. 更改根節點(響應)wcf服務
- 8. 根據服務器響應動態加載圖像列表
- 9. 使用服務器響應中的按鈕創建Android佈局
- 10. 更改按鈕的位置
- 11. 更改按鈕的位置
- 12. 如何根據位置更改Listview按鈕的背景圖像?
- 13. 按鈕在android中的響應定位
- 14. 如何根據服務器響應
- 15. 如何根據recyclerview Android的位置動態地更改佈局中的元素?
- 16. 在動態佈局中更改位置按鈕
- 17. Java Android動態更改ImageView的位置
- 18. 更改按鈕位置
- 19. 根據滾動位置動態更改href
- 20. 根據URL更改UIWebView滾動位置
- 21. 如何動態更改按鈕的位置
- 22. DateTime未響應服務器更改
- 23. OWIN更改響應服務器標識?
- 24. 如何根據按鈕點擊動態改變imagview邊距android
- 25. AngularJS:智能表根據來自服務器的http響應動態更改表格內容
- 26. 按下時更改按鈕的位置
- 27. 更改在TFS中自動生成的服務器位置
- 28. Ajax或JavaScript:根據服務器響應改變樣式
- 29. 自動修改服務器響應
- 30. JavaFx手動更改按鈕的位置或位置?
你想爲它設置X和Y嗎? –
nope它會像按鈕w按鈕x按鈕y按鈕z – alibaba
爲什麼不動態設置'按鈕'文本代替改變按鈕的位置? – NightWatcher