Android 以编程方式创建按钮到新行
我创建了一个布局,通过编程创建按钮(请参见代码)。使用这种布局,我有一列按钮,但我会创建类似下图的内容。我尝试使用线性布局,但按钮没有自动转到新行,也没有显示,所以我将其更改为表格布局。如何以编程方式创建类似于图片的内容 多谢各位Android 以编程方式创建按钮到新行,android,button,layout,Android,Button,Layout,我创建了一个布局,通过编程创建按钮(请参见代码)。使用这种布局,我有一列按钮,但我会创建类似下图的内容。我尝试使用线性布局,但按钮没有自动转到新行,也没有显示,所以我将其更改为表格布局。如何以编程方式创建类似于图片的内容 多谢各位 protected TableLayout layout; LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
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);
}
受保护的表格布局;
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_内容,ViewGroup.LayoutParams.WRAP_内容);
布局=(TableLayout)findViewById(R.id.workflows\u activity\u布局);
私有void createButton(){
加载.setVisibility(视图.VISIBLE);
layout.removeallview();
如果(items.length==0){
TableRow row=新的TableRow(本);
TextView no_item=新的TextView(此);
没有问题。setText(“没有项目”);
无问题。setTextSize(35);
行.添加视图(无项目);
布局。添加视图(行);
}否则{
对于(int i=0;i
例如,您可以使用labraries来实现这一点
用法:
<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>
<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" />
另一个解决办法是:
用法:
<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>
<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 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\u lay=(TableLayout)view.findViewById(R.id.tab\u lay);
对于(int i=1;我需要以编程方式用代码实现它,而不是用xml。可以吗?您可以以编程方式创建此FlowLayout
s,并将您的按钮添加到其中。我不知道如何执行此操作。这是我需要的,但我如何从数组中读取项的数量(从数组\u item.lenght)并使用它?