How to apply attributes of a parent Viewgroup to child in android studio?

Asked

Viewed 123 times

4

How to make children of a Viewgroup inherit a parent attribute? For example, I would like to set up layout_marginTop="16dp" for all Textview daughters

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="16dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quantity"
        android:textAllCaps="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quanty_text_view"
        android:textColor="#000000"
        android:textSize="16sp"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/order" />
</LinearLayout>

2 answers

1


Dear,

I strongly advise you to use the resources Styles, dimen, Colors and strings. With them it is possible to configure "templates" and just call by name inside the layout. For example: If you had to do 10 Textviews identicas you could make only one template and use all at once:

<style name="nakamoto">
    <item name="android:textStyle">bold</item>
    <item name="android:textSize">24sp</item>
    <item name="android:paddingTop">8dp</item>
    <item name="android:paddingBottom">8dp</item>
    <item name="android:gravity">center_vertical</item>
</style>

To access the XML of Styles just follow the path as shown below: inserir a descrição da imagem aqui

Then just assign the name in the desired Textviews using the correct function and the name you gave the "matrix".

inserir a descrição da imagem aqui

  • 1

    Thank you so much! That’s exactly what I needed! android: at the beginning of the sentence?

  • @Nakamoto I don’t see how this is what you asked.

  • Not in this case, that’s right.

0

To my knowledge, it will only be possible to implement this behavior in a class inherited from Textview.

public class TextViewWithParentTopMargin extends android.support.v7.widget.AppCompatTextView {
    public TextViewWithParentTopMargin(Context context) {
        super(context);
    }

    public TextViewWithParentTopMargin(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TextViewWithParentTopMargin(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup parent = (ViewGroup) getParent();
        ViewGroup.MarginLayoutParams parentLayoutParams = (ViewGroup.MarginLayoutParams)parent.getLayoutParams();

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();

        int leftMargin = layoutParams.leftMargin;
        int topMargin = parentLayoutParams.topMargin;//Atribui valor do pai ao TextView
        int rightMargin = layoutParams.rightMargin;
        int bottomMargin = layoutParams.bottomMargin;
        layoutParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
    }
}

In the method onMeasure() get Marginlayoutparams from the father and assign his margins to the margins of Textview.

In the example given only was attributed to topMargin.

The xml will look like this:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingTop="16dp"
    android:layout_marginTop="50dp">

    <sua.package.TextViewWithParentTopMargin
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/quantity"
        android:textAllCaps="true"/>

    ...

</LinearLayout>

Textview will be automatically applied (will inherit) the value of layout_marginTop from his father to his top margin.

  • I believe I’m not clarifying the doubt. Anyway, thanks for the support!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.