Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.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 将布局的自定义属性添加到子级_Android - Fatal编程技术网

Android 将布局的自定义属性添加到子级

Android 将布局的自定义属性添加到子级,android,Android,我有布局 <android.support.v7.widget.GridLayout android:id="@+id/grid_layout" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="@dimen/default_grid_height" xmlns:ap

我有布局

<android.support.v7.widget.GridLayout
    android:id="@+id/grid_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/default_grid_height"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <!--I need add to this child attribute "app:layout_*"-->
    <Button
        android:text="1"
        app:layout_columnSpan="2"
        app:layout_columnWeight="2"
        app:layout_rowWeight="3"/>

</android.support.v7.widget.GridLayout>

GridLayout
中添加类似的内容并不困难。但你需要知道你想补充什么

GridLayout gv = (GridLayout) findViewById(R.id.grid_layout);
TextView tv = new TextView(context);
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
tv.setText("TextView");
LayoutParams params = new LayoutParams(Set your column and row information as params);
tv.setLayoutParams(params);
gv.addView(tv);

我的建议是,我们可以让它变得非常简单,只需在自定义类中为此创建一个方法

如果您使用XML进行设置,您可以像下面这样调用
app:layout\u rowWeight
,这样您就可以在TypedArray中进入自定义类

但是对于Jave,您可以创建一个单独的方法,并且可以实现相同的效果


查看Android源代码,然后搜索
setTextColor
方法。

好的,我知道如何将子视图添加到某些视图组。如何将自定义布局参数设置为此视图组的子级?在我的例子中,param=“app:layout_columnSpan=3”和ViewGroup=GridLayout来自支持库
param.columnSpec=GridLayout.spec(A);param.rowSpec=GridLayout.spec(B)
GridLayout gv = (GridLayout) findViewById(R.id.grid_layout);
TextView tv = new TextView(context);
tv.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);
tv.setText("TextView");
LayoutParams params = new LayoutParams(Set your column and row information as params);
tv.setLayoutParams(params);
gv.addView(tv);