2017-01-02 104 views
0

我試圖以編程方式設置GridView內的ToggleButton。所有似乎都在工作,但我不能設置一些屬性,例如我添加到循環中的togglebutton和這個屬性之間的邊距:toogleBtn.setAllCaps(false);不工作。GridView不要以編程方式在ToggleButton中設置頁邊距

在此先感謝

這是我的xml:

<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> 

這是我在java類代碼:

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

在切換按鈕裏面你的XML文件中設置這些屬性

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

如果你不減少在xml的最小寬度,那麼你有多少嘗試減少其寬度&高度它將無法正常工作。我遭受了這個問題2天

+0

嗨@Redman。我並未在xml中添加toogle按鈕。我在代碼中以編程方式添加。看看我的xml在我的問題 –

+0

請嘗試保持此狀態,看看是否工作'toogleBtn.setMinimumHeight(10)' 'toogleBtn.setMinimumWidth(10)' – Redman

+0

不工作此解決方案 –

相關問題