2017-01-02 59 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

不工作此解决方案 –

相关问题