2017-01-02 4 views
0

ToggleButtonをプログラム内でGridview内に設定しようとしています。すべてが動作しているようですが、私はループ内でこのトグルボタンとこのプロパティの間に余白のようないくつかのプロパティを設定できません:toogleBtn.setAllCaps(false);動作しません。これは、Javaのクラスで私のコードGridViewでToggleButtonの余白をプログラムで設定しない

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    style="@style/screen" 
    android:id="@+id/ly_general" 
    android:fitsSystemWindows="true" 
    android:background="@color/colorPrimary"> 

    <TextView 
     android:text="@string/your_interest" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@color/white" 
     android:textSize="@dimen/text_size_22" 
     android:id="@+id/text_init_session" 
     android:layout_marginTop="@dimen/margin_26" 
     android:layout_marginLeft="@dimen/padding_20" 
     android:layout_below="@+id/image"/> 

    <TextView 
     android:text="@string/select_categories" 
     android:textColor="@color/white" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text_init_session" 
     android:layout_alignLeft="@+id/text_init_session" 
     android:layout_alignStart="@+id/text_init_session" 
     android:layout_marginTop="22dp" 
     android:id="@+id/textViewExplanation" /> 
<ScrollView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fillViewport="true" 
     style="@style/screen" 
     android:id="@+id/scroll" 
     android:layout_below="@+id/textViewExplanation"> 

     <GridLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/lyCategory" 
      android:layout_gravity="center_horizontal" 
      android:orientation="horizontal" 
      android:columnCount="3" 
      android:layout_marginTop="@dimen/padding_40"> 
     </GridLayout> 
</ScrollView> 
</RelativeLayout> 

です:事前に

おかげで

これが私のxmlです設定トグルボタン内部のXMLファイルで

for (int i = 0; i < arrayList.size(); i++) { 
      final ToggleButton toogleBtn = new ToggleButton (RegisterThreeActivityNew.this); 
      LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT, GridLayout.LayoutParams.WRAP_CONTENT); 
      params.setMargins(20, 20, 20, 20); 

     toogleBtn.setLayoutParams(params); 
     toogleBtn.setWidth(140); 
     toogleBtn.setHeight(20); 
     toogleBtn.setTextSize(12); 
     toogleBtn.setAllCaps(false); 
     toogleBtn.setText(arrayList.get(i).getName()); 
     toogleBtn.setTextOff(arrayList.get(i).getName()); 
     toogleBtn.setTextOn(arrayList.get(i).getName()); 
     toogleBtn.setTextColor(ResourcesCompat.getColor(getResources(), R.color.white, null)); 
     toogleBtn.setBackground(ResourcesCompat.getDrawable(getResources(), R.drawable.btn_category, null)); 


     layoutCategories.addView(toogleBtn); 
    } 

答えて

0

は、これらのプロパティ

android:minWidth="10dp" // use what ever minimum value you need 
    android:minHeight="10dp" 

xmlの最小幅を小さくしないと、幅を狭くしようとする度に&の高さでは動作しません。この問題は2日間続きました

+0

こんにちは@Redman。私はXMLのボタンをGoogleに追加していません。私はプログラムでコードに追加しています。私の質問で私のXMLを見てください –

+0

これを保ち、 'toogleBtn.setMinimumHeight(10)' 'toogleBtn.setMinimumWidth(10)' – Redman

+0

がこの解決策を働かないかどうかを調べてみてください –

関連する問題