简体   繁体   English

如何在彼此之间动态添加按钮

[英]How to add buttons below each other dynamically

For the last few days, I've been trying to figure out how to make a list of buttons that are below each other, and fill the entire width of the screen dynamically. 在过去的几天中,我一直在尝试找出如何制作一个位于彼此下方的按钮列表,以及如何动态填充屏幕的整个宽度。 So for example: 因此,例如:

堆叠按钮的图像,显​​然我还不能嵌入图像。

(I cheaply just took an pre-existing screen shot that had one button and edited it in paint.net to make it look like the buttons are stacked.) (我便宜地拍摄了一个有一个按钮的预先存在的屏幕快照,并在paint.net中对其进行了编辑,以使其看起来像是在堆叠按钮。)

So, essentially, I have a list of strings from R that include the button's names and I need to list them like that. 因此,从本质上讲,我有一个来自R的字符串列表,其中包括按钮的名称,我需要像这样列出它们。 I've been looking around and if I did find anything it was outdated (and didn't work) 我一直在环顾四周,如果发现任何过时的东西(没有用)

This is what I have so far (its a little messy because it contains many attempts at making this happen) 到目前为止,这就是我所拥有的(有点混乱,因为它包含许多尝试使这种情况发生的尝试)

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

(this is in onCreate() fyi) (这在onCreate() fyi中)

So, if anyone has any solutions/ideas on how I can do this, please share. 因此,如果有人对我的操作有任何解决方案/想法,请分享。 I'm fairly new to creating android applications. 我是创建Android应用程序的新手。

Java Java的

    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 XML

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

A simple example 一个简单的例子

XML: 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>

Java: Java的:

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

You might be better off learning how to use ListViews , and using Adapters to populate the ListView with Buttons containing the data from your array. 学习如何使用ListViews以及使用适配器用包含来自数组的数据的Button填充ListView可能会更好。

Bear in mind that for the solution you and Sajith Sageer offered, when there are too many buttons created, the buttons will extend past the screen, making them unreachable unless you wrap the LinearLayout in a ScrollView. 请记住,对于您和Sajith Sageer提供的解决方案,当创建的按钮过多时,这些按钮将超出屏幕,除非将LinearLayout包装在ScrollView中,否则它们将不可访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM