0

I have layout

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

I need to programmatically add child to this layout container (support.v7.widget.GridLayout) with custom attributes supported by this layout. How i can do this? Assume I can do something like that, but not found something method like tihs.

widget = CustomWidget(context)
widget.addParam("app:layout_rowWeight", 3) // how??
gprathour
  • 14,813
  • 5
  • 66
  • 90
whalemare
  • 1,107
  • 1
  • 13
  • 30

2 Answers2

1

It isn't difficult to add something to GridLayout like this. But you need to know what do you want to add.

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);
Mehran Zamani
  • 831
  • 9
  • 31
  • Okay, i know how add child views to some ViewGroup. How i can set *custom layout params* to child of this ViewGroup? In my case param = "app:layout_columnSpan = 3", and ViewGroup = GridLayout from support library – whalemare Jul 09 '17 at 06:24
  • `param.columnSpec = GridLayout.spec(A); param.rowSpec = GridLayout.spec(B);` – Mehran Zamani Jul 09 '17 at 06:46
0

My suggestion is, we can make it very simple, Just create one method in your custom class for that,

If you are setting in XML you can call like this app:layout_rowWeight so that you can get in your Custom class in TypedArray.

But for Jave, you can create a separate method and you can achieve same.

Ex Check TextView Android source code, and search setTextColor method.

Muthukrishnan Rajendran
  • 11,122
  • 3
  • 31
  • 41