2016-03-21 10 views
0

我正在開發一個Android應用程序。在我的應用程序中,我將複選框以編程方式添加到LinearLayout。但添加複選框後,它不適合佈局。請參閱下面的屏幕截圖。以編程方式添加帶標題的複選框不適合Android中的佈局

截圖

enter image description here

正如你在截圖 「X-samll」 文本及其複選框看到沒有正確安裝。我想要的是當沒有足夠的空間時,複選框和文本一起轉到新行。請如何實現它。

這是我如何編程方式添加複選框

if(attrs.length()>0) 
           { 
            LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext()); 
            attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL); 
            LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
            attrControlsSubContainer.setLayoutParams(layoutParams); 
            for(int i=0;i<attrs.length();i++) 
            { 
             CheckBox chkAttribute = new CheckBox(getBaseContext()); 
             chkAttribute.setText(attrs.getJSONObject(i).getString("name")); 
             chkAttribute.setTextColor(Color.BLACK); 
             chkAttribute.setId(attrs.getJSONObject(i).getInt("id")); 
             attrControlsSubContainer.addView(chkAttribute); 
            } 
            attributeControlContainer.addView(attrControlsSubContainer); 
           } 
+0

相關[問題](http://stackoverflow.com/questions/ 14528381/android-horizo​​ntal-linearlayout-wrap-elements) – OJ7

回答

0

使用下面的代碼:

if(attrs.length() > 0) { 
     ScrollView mScrollView = new HorizontalScrollView(getApplicationContext()); 
     mScrollView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
     mScrollView.setFillViewport(true); 
     LinearLayout attrControlsSubContainer = new LinearLayout(getBaseContext()); 
     attrControlsSubContainer.setOrientation(LinearLayout.HORIZONTAL); 
     LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);        attrControlsSubContainer.setLayoutParams(layoutParams); 
     for(int i=0;i<attrs.length();i++) 
     { 
     CheckBox chkAttribute = new CheckBox(getBaseContext()); 
     chkAttribute.setText(attrs.getJSONObject(i).getString("name")); 
     chkAttribute.setTextColor(Color.BLACK); 
     chkAttribute.setId(attrs.getJSONObject(i).getInt("id")); 
     attrControlsSubContainer.addView(chkAttribute); 
            } 
     mScrollView.addView(attrControlsSubContainer); 
     attributeControlContainer.addView(mScrollView); 


} 
+0

這不起作用。它也是一樣的。 –

0

LinearLayout是不夠的,這一點,你必須使用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"> 


</com.wefika.flowlayout.FlowLayout> 

搖籃扶養: - compile 'org.apmem.tools:layouts:[email protected]'

然後加入動態複選框FlowLayout

使用FlowLayout.LayoutParams

FlowLayout.LayoutParams params = new FlowLayout.LayoutParams 
        (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
+0

我可以通過鏈接使用Grandle安裝FlowLayout嗎? –

相關問題