以编程方式添加的带标题的复选框不适合Android

时间:2016-03-21 16:21:00

标签: android android-checkbox

我正在开发一个Android应用程序。在我的应用程序中,我以编程方式将复选框添加到LinearLayout。但是在添加复选框后,它不适合布局。请参阅下面的屏幕截图。

  

截图

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

2 个答案:

答案 0 :(得分: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);  


 }

答案 1 :(得分: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>

Gradle Dependancy : - compile 'org.apmem.tools:layouts:1.10@aar'

然后在FlowLayout

中动态添加复选框

使用 FlowLayout.LayoutParams

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