2016-03-21 6 views
0

私はアンドロイドアプリを開発中です。私のアプリケーションでは、プログラムで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-linearlayoutout-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> 

Gradleの依存: - 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をインストールするリンクを取得できますか? –

関連する問題