以编程方式在RelativeLayout中设置边距tooglebutton

时间:2017-01-10 09:29:41

标签: android grid-layout

我在ToggleButton动态创建GridView。我可以显示我从服务器发送的所有ToggleButton。唯一的问题是我无法设置任何余量来将ToggleButton分开。我是以编程方式添加ToggleButton的。

我需要使用gridlayout。

如何以编程方式在ToggleButton之间设置边距。

类别:

  for (int i = 0; i < arrayList.size(); i++) {
            final ToggleButton toogleBtn = new ToggleButton (RegisterThreeActivityNew.this);
            toogleBtn.setPadding(50,10,30,20);
            toogleBtn.setWidth(140);
            toogleBtn.setHeight(20);
            toogleBtn.setTextSize(12);
            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));

            final long id = arrayList.get(i).getId();
            toogleBtn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        toggleEnabled(toogleBtn);
                        categories.add(id);
                    } else {
                        toggleDissabled(toogleBtn);
                        categories.remove(id);
                    }
                }
            });
            gridLayout.addView(toogleBtn);
        }

XML:

<?xml version="1.0" encoding="utf-8"?>
<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">



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

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

        <include layout="@layout/footer"
        android:id="@+id/include"/>

    <ImageView
        android:id="@+id/image"
        android:layout_width="134dp"
        android:layout_height="45dp"
        android:src="@drawable/logo"
        android:layout_alignParentTop="true"
        android:layout_alignLeft="@+id/text_init_session"
        android:layout_alignStart="@+id/text_init_session" />
</RelativeLayout>

0 个答案:

没有答案