0

我有两种方法:

public View addNewLinearLayout(Context context) {
    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.height = 120;
    linearLayout.setLayoutParams(params);
    linearLayout.setBackgroundColor(Color.RED);
    linearLayout.setGravity(Gravity.TOP);
    linearLayout.addView(buttonsGenerator(context));
    return linearLayout;
}
public View buttonsGenerator(Context context){
    ListView lst;
    lst = new ListView(context);

    Button button = new Button(context);

    return lst;
}

在 addNewLinearLayout 中,我添加了要查看的布局。在buttonsGenerator 中,我想将一些按钮添加到列表中,并在我的布局中从addNewLinearLayout 中添加带有按钮的列表。我如何创建按钮列表并使用方法buttonGenerator将它们添加到线性?

4

1 回答 1

1

例如:

public View addNewLinearLayout(Context context) {
    LinearLayout linearLayout = new LinearLayout(context);
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    params.height = 120;
    linearLayout.setLayoutParams(params);
    linearLayout.setBackgroundColor(Color.RED);
    linearLayout.setGravity(Gravity.TOP);

    List<View> components = getButtons(context);
    for(View component : components) {
       linearLayout.addView(component);
    }

    return linearLayout;
}

public List<View> getButtons(context) {
    List<View> buttons = new ArrayList<View>();
    for(int i = 0; i < 10; i++) {
       buttons.add(createButton(context));
    }
    return buttons;
}

public View createButton(Context context){
    ListView lst;
    lst = new ListView(context);

    Button button = new Button(context);

    return lst;
}

但最好使用列表视图而不是这个。

于 2013-02-12T08:14:52.290 回答