【问题标题】:Create buttons programmatically goes new line以编程方式创建按钮换行
【发布时间】:2015-11-09 07:51:57
【问题描述】:

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

谢谢。

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

【问题讨论】:

    标签: android button layout


    【解决方案1】:

    有一种简单的方法可以实现您想要的。看这段代码:

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

    如果您想要更多按钮,则只需将与 i 比较的 4 更改为您的值。希望能帮上忙。

    【讨论】:

    • 是我需要的,但我如何从数组中读取项目数(来自 array_item.lenght)并使用它?
    【解决方案2】:

    例如,您可以使用实验室来实现这一点

    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" />
    

    【讨论】:

    • 我需要使用代码以编程方式实现它,而不是使用 xml。可以吗?
    • 您可以通过编程方式创建此FlowLayouts 并将您的按钮添加到其中
    • 我不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2011-06-29
    • 2014-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多