Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式在RelativeLayout内设置边距tooglebutton_Android_Grid Layout - Fatal编程技术网

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

Android 以编程方式在RelativeLayout内设置边距tooglebutton,android,grid-layout,Android,Grid Layout,我正在网格视图中动态创建一个切换按钮。我可以显示从服务器发送的所有ToggleButton。唯一的问题是我无法设置任何边距来分隔它们之间的ToggleButtons。我正在以编程方式添加ToggleButtons 我需要用gridlayout来做 如何以编程方式设置ToggleButtons之间的边距 类别: for (int i = 0; i < arrayList.size(); i++) { final ToggleButton toogleBtn = n

我正在
网格视图
中动态创建一个
切换按钮
。我可以显示从服务器发送的所有
ToggleButton
。唯一的问题是我无法设置任何边距来分隔它们之间的
ToggleButton
s。我正在以编程方式添加
ToggleButton
s

我需要用gridlayout来做

如何以编程方式设置
ToggleButton
s之间的边距

类别:

  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 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>
for(int i=0;i
XML:

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


的可能重复不是同一个问题。我需要它在GridLayout和这个解决方案不适合我是的,它是。。您需要使用
GridLayout.LayoutParams
而不是
LinearLayout.LayoutParams
。。