如何动态地添加彼此下方的按钮

时间:2015-10-25 22:59:54

标签: java android

在过去的几天里,我一直在试图弄清楚如何制作一个彼此下方的按钮列表,并动态地填充整个屏幕宽度。例如:

Image of stacked buttons, apparently I cant embed images yet.

(我便宜地拍摄了一个预先存在的屏幕截图,其中有一个按钮,并在paint.net中进行了编辑,使其看起来像按钮堆叠。)

所以,基本上,我有一个来自R的字符串列表,其中包含按钮的名称,我需要像这样列出它们。我一直在四处寻找,如果我找到任何东西,它已经过时(并且没有用)

这是我到目前为止(它有点乱,因为它包含许多尝试来实现这一点)

LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);

    Field[] fields = R.raw.class.getFields();

    for(int count=0; count < fields.length; count++){

        String filename = fields[count].getName();

        try {
            Button button = new Button(this);
            button.setText(filename);
            button.setId(startID + 1 + count); //this variable is offscreen
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //finish later
                }

            });
            button.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            layout.addView(button);

        } catch (Exception e) {
            Toast.makeText(MainActivity.this, "Exception.", Toast.LENGTH_LONG).show();
        }
    }

(这是onCreate() fyi)

所以,如果有人对我如何做到这一点有任何解决方案/想法,请分享。我是创建Android应用程序的新手。

3 个答案:

答案 0 :(得分:1)

爪哇

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);

    String[] items = {"item 1", "item 2", "item 3", "item 4", "item 5"};

    for (int i = 0; i < items.length; i++) {
        Button btn = new Button(this);
        btn.setText(items[i]);
        btn.setId(i);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        params.setMargins(10, 10, 10, 10);
        btn.setLayoutParams(params);
        layout.addView(btn);
    }

XML

   <LinearLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
  </LinearLayout>

答案 1 :(得分:0)

一个简单的例子

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:orientation="vertical">
    </LinearLayout>

</RelativeLayout>

爪哇:

String[] names = { "button1", "button2", "button3", "button4" };

LinearLayout layout = (LinearLayout) findViewById(R.id.linear);

for (int i = 0; i < 4; i++) {
    Button button = new Button(this);
    button.setText(names[i]);
    button.setId(i);
    button.setBackgroundColor(Color.BLUE);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.setMargins(20, 0, 20, 20); // (left, top, right, bottom)
    button.setLayoutParams(params);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // finish later
        }

    });
    layout.addView(button);

答案 2 :(得分:0)

您可能最好学习如何使用ListViews,并使用适配器使用包含数组中数据的按钮填充ListView。

请记住,对于您和Sajith Sageer提供的解决方案,当创建的按钮太多时,按钮将延伸到屏幕之外,使得它们无法访问,除非您将LinearLayout包装在ScrollView中。