2016-03-21 26 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吗? –

相关问题