以编程方式创建按钮是新的一行

时间:2015-11-08 20:38:45

标签: android button layout

我创建了一个以编程方式创建按钮的布局(参见代码)。有了这个布局,我有一列按钮,但我会创建如下图所示的内容。我尝试了线性布局的东西,但按钮没有自动转到新行并且没有显示,所以我将其更改为表格布局。如何以编程方式创建图片?

layout that I would like

谢谢。

protected TableLayout layout;

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

layout = (TableLayout) findViewById(R.id.workflows_activity_layout);

private void createButton() { 
    loading.setVisibility(View.VISIBLE);
    layout.removeAllViews();

    if (items.length == 0) {
        TableRow row = new TableRow(this);
        TextView no_item = new TextView(this);
        no_questions.setText("there are no items");
        no_questions.setTextSize(35);
        row.addView(no_item);
        layout.addView(row);
    } else {

        for (int i = 0; i < items.length; i++) {
            TableRow row = new TableRow(this);
            final Button btn = new Button(this);
            TextView tw = new TextView(this);
            tw.setText(items[i].getName());
            tw.setTextSize(25);
            btn.setBackgroundResource(R.drawable.button_image);
            btn.setId(i);
            btn.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {  
                    int btn_selected = btn.getId();
                    Intent openItempage = new Intent(MainActivity.this, ItemsActivity.class);
                    startActivity(openItempage);
                }
            });
            row.addView(btn);
            row.addView(tw);
            layout.addView(row, params);
            items_buttons.add(btn);
            items_nameText.add(tw);
        }
    }

    loading.setVisibility(View.GONE);
}

2 个答案:

答案 0 :(得分:2)

有一种简单的方法可以达到你想要的效果。看看这段代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TableLayout
    android:id="@+id/tab_lay"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:stretchColumns="*"
    android:gravity="center_vertical" />

TabLayout tab_lay = (TableLayout)view.findViewById(R.id.tab_lay);
for(int i = 1; i <= 4; ){
        TableRow a = new TableRow(getActivity());
        TableRow.LayoutParams param = new TableRow.LayoutParams(
                TableRow.LayoutParams.FILL_PARENT,
                TableRow.LayoutParams.WRAP_CONTENT, 1.0f);
        a.setLayoutParams(param);
        a.setGravity(Gravity.CENTER_VERTICAL);
        for(int y = 0; (y < 2) && (i <= 4); y++) {
            Button x = new Button(getActivity());
            x.setText("Text " + i);
            x.setGravity(Gravity.CENTER);
            TableRow.LayoutParams par = new TableRow.LayoutParams(y);
            x.setLayoutParams(par);
            int ids = i;
            x.setId(ids);
            x.setOnClickListener(this);
            a.addView(x);
            i++;
        }
        tab_lay.addView(a);
}

如果你想要更多按钮,那么只需将4与你的值进行比较即可。希望我能帮忙。

答案 1 :(得分:0)

您可以使用labraries实现此功能,例如

FlowLayout

用法:

<com.wefika.flowlayout.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="start|top">

    <View
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum" />

</com.wefika.flowlayout.FlowLayout>

另一种解决方案是:

AndroidFlowLayout

用法:

<com.liangfeizc.flowlayout.FlowLayout
    android:id="@+id/flow_layout"
    flowlayout:vertical_spacing="10dp"
    flowlayout:horizontal_spacing="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />