如何在表格布局中将数组值显示为按钮?

时间:2013-11-25 13:31:00

标签: android android-tablelayout

我是android新手。我需要将字母数组的值(A-Z)显示为按钮,但如果我使用线性布局,按钮将以线性线显示,导致在布局中无法看到许多按钮。请帮助我,使用表格布局可以看到按钮。 Ť

enter image description here

这是我的代码:

public class Alphabet extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //LinearLayOut Setup
        LinearLayout linearLayout= new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);

        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));


        //TableLayout Setup
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setOrientation(tableLayout.HORIZONTAL);

        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));

        List<String> alpha = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                "U", "V", "W", "X", "Y", "Z");

        for(String item : alpha ){
            System.out.println(item);
            Button btn = new Button(this);
            btn.setText(item);
            btn.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            linearLayout.addView(btn);
        }

        setContentView(linearLayout);
    }

}   

0 个答案:

没有答案