Android - 在现有布局中动态添加按钮

4
我正在构建一个音效板应用程序,其中我在表格布局中有一些预定义按钮,它嵌套在相对布局内。该布局是自己的片段(我使用多个选项卡来表示不同的类别)。
声音和选项卡已经正常工作,现在我想要实现这样的功能,即一旦您单击“+”按钮,便生成一个新按钮,并且“+”按钮向下移动一行。
是否有一种方法可以生成新的表格布局,并具有与已有布局相同的属性,而不必在xml文件中使用空白?

1
你能发布这个UI的布局吗? - betteroutthanin
图片很好,但实际上如果您发布一些代码会更好。 - Blackbelt
使用两个RelativeLayout,一个用于按钮,另一个用于+按钮(具有下面的属性),并在第一个RelativeLayout中添加一个带有下面最后一个按钮ID的属性的按钮... - Pramod
我在尝试通过编程方式将新的布局添加到现有布局中,但遇到了问题。 - Stef
3个回答

2

尝试这个简单的示例,并根据您的要求进行修改。

 public class MainActivity extends Activity {
        Context context;
        ScrollView scrollView;
        LinearLayout container;
        LinearLayout layout;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            context = this;
            setContentView(R.layout.activity_main);
             layout = (LinearLayout) findViewById(R.id.LinearAdd);


        }

        public void addButton(View view) {
            Button button  = new Button(MainActivity.this);
            LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT );
            button.setLayoutParams(lp);
            button.setText("ADDED");
            layout.addView(button);
        }

    }

And this your xml

<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:id="@+id/LinearAdd"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="70"
        android:orientation="vertical" >

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addButton"
            android:text="click to add" />
    </LinearLayout>

</LinearLayout>

如果我想将这段代码从MainActivity中移到一个单独的Activity中,只用于生成按钮,那么我该如何从我的Fragment内部调用该函数? - Stef
使用此XML为您的片段类设置视图,并将单击方法放入您的片段类中。那会起作用。 - user3217803

0
这是我的布局片段代码:
<?xml version="1.0" encoding="utf-8"?>

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

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:id="@+id/tableLayout">


    <TableRow
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_column="1">
        <Button
            android:background="@drawable/button"
            style="@style/button_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tab01sound01"
            android:layout_column="0"
            android:text="@string/buttonAlan" />
    </TableRow>

</TableLayout>

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*"
    android:layout_below="@+id/tableLayout2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:id="@+id/thirdTable">

</TableLayout>

</RelativeLayout>

0

这是我第一个片段标签的代码

public class Tab1 extends Fragment {

private int buttonAmount = MainActivity.getButtonAmountTab1();
private Button[] button = new Button[buttonAmount + 1];
private Sound sound;
private String packageName = MainActivity.getStringPackageName();
private Button addButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    sound = MainActivity.getSound();

    View rootView = inflater.inflate(R.layout.fragment_fun, container, false);

    //This generates the Audio functionality for each button

    for (int i = 1; i < buttonAmount + 1; i++) {
        String buttonID = "tab01sound0" + i;
        int resID = getResources().getIdentifier(buttonID, "id", packageName);
        final int audioID = getResources().getIdentifier(buttonID, "raw", packageName);
        button[i] = (Button) rootView.findViewById(resID);
        registerForContextMenu(button[i]);
        button[i].setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                sound.playSound(audioID);
            }

        });

    }

    //functionality for the "+" button

    addButton = (Button) rootView.findViewById(R.id.addButton);
    addButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            CreateDialog newDialog = new CreateDialog();
            newDialog.AlertBox(getActivity());

        }
    });

    return rootView;

}

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接