我想了一堆按鈕添加到像這樣的佈局:如何設置一個按鈕的參數編程
for(int i = 0; i < 10; i++) {
Button button = new Button(this);
button.setText("" + i);
((LinearLayout)dialog.findViewById(R.id.Buttons)).addView(button);
}
我的問題是我怎麼做到這一點編程到所有的按鈕:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="32dip" />
我一直在看LayoutParams,但它看起來不完整。像我如何設置textSize爲32傾角?
雖然我在引力方面遇到了問題。 setGravity(Gravity.CENTER_HORIZONTAL)在xml文件中的作用與android:layout_gravity =「center_horizontal」不同。 xml文件按鈕居中,但不是使用setGravity()創建的按鈕。 – Espen
嘗試使用佈局參數:LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,Gravity.CENTER_HORIZONTAL); – dymmeh
或初始化params調用後:params.gravity = Gravity.CENTER_HORIZONTAL; – dymmeh