whalemare
whalemare

Reputation: 1326

Add custom attributes of layout to children

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

Upvotes: 0

Views: 304

Answers (2)

Mehran Zamani
Mehran Zamani

Reputation: 831

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

Upvotes: 1

Muthukrishnan Rajendran
Muthukrishnan Rajendran

Reputation: 11642

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.

Upvotes: 0

Related Questions