2015-11-08 41 views
0

我創建了一個以編程方式創建按鈕的佈局(請參閱代碼)。有了這個佈局,我有一列按鈕,但我會創建類似下面的圖片。我嘗試了線性佈局,但按鈕沒有自動換行並且沒有顯示,所以我將其更改爲表格佈局。我如何以編程方式創建類似圖片的內容?以編程方式創建按鈕新行

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

有一個簡單的方法來實現你想要的。看看這個代碼:

<?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是比較我給你的價值。希望我幫忙。

+0

是我需要的,但我怎樣才能從數組(從array_item.lenght)讀取項目的數量並使用它? – pnappa

+0

我解決了它。謝謝! – pnappa

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

我需要用代碼以編程方式實現它,而不是使用xml。你可以嗎? – pnappa

+0

您可以通過編程方式創建這個'FlowLayout'並將其添加到您的按鈕 –

+0

我不知道該怎麼做。 – pnappa

相關問題