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

4

How do children in a ViewGroup inherit an attribute from the parent? For example, I would like to set layout_marginTop="16dp" to 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>

    
asked by anonymous 27.10.2017 / 15:39

2 answers

1

Dear,

I strongly advise you to use the styles, dimen, colors, and strings features. With them it is possible to configure "models" and just call by the name inside the layout. For example: If you had to do 10 identical TextViews you could just make one model and use it 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 styles XML simply follow the path as shown below:

ThenyoujustassignthenameinthedesiredTextViewsusingthecorrectfunctionandthenamethatgavethe"array".

    
28.10.2017 / 04:46
0

As far as I know, it will only be possible to implement this behavior in a TextView inheritance class.

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 onMeasure() method, get the parent's MarginLayoutParams and assign their margins to the edges of the TextView.

In the given example, only topMargin was assigned.

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 automatically apply (inherit) the value of layout_marginTop from parent to its top margin .

    
27.10.2017 / 16:49