[英]Programmatically added checkbox with title does not fit in the layout in Android
I am developing an Android app.我正在开发一个 Android 应用程序。 In my app I am adding checkboxes programmatically to a LinearLayout.
在我的应用程序中,我以编程方式将复选框添加到 LinearLayout。 But after the checkboxes are added, it does not properly fit to the layout.
但是在添加复选框后,它不能正确地适应布局。
Screenshot:截图:
As you can see in screenshot "x-samll" text and its checkbox are not fitted properly.正如您在屏幕截图中看到的“x-samll”文本及其复选框未正确安装。 What I want is both checkbox and its text together go to new line when there is not enough space.
当没有足够的空间时,我想要的是复选框及其文本一起转到新行。 How can I achieve it?
我怎样才能实现它?
This is how I programmatically add checkboxes:这就是我以编程方式添加复选框的方式:
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);
}
use below code:使用以下代码:
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);
}
LinearLayout
is not enough for this you must use FlowLayout 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'
Gradle 依赖:-
compile 'org.apmem.tools:layouts:1.10@aar'
Then add check box dynamically in FlowLayout
然后在
FlowLayout
中动态添加复选框
use FlowLayout.LayoutParams使用FlowLayout.LayoutParams
FlowLayout.LayoutParams params = new FlowLayout.LayoutParams
(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.