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