简体   繁体   中英

set child params from parent custom viewGroup

I have a CustomViewGroup with xml like this:

<?xml version="1.0" encoding="utf-8"?>
 <merge xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:gravity="center"
 android:orientation="horizontal">         

 <View      
  android:layout_width="?"     
  android:layout_height="?"
  android:id="@+id/child"
  />

</merge>

and I want to use parent like this

 <CustomViewGroup
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/parent"
    app:itemWidth="30dp"
    app:itemHeight="30dp"
    />

as you can see I want to read my child width and height from parent attribute. is it possible?
something like, read from parent theme right? when you set a value like an item background from the parent theme.

 android:background="?attr/selectableItemBackground"

Yes, this is possible. Here's the necessary Java code for CustomViewGroup ... it's really not too bad:

public class CustomViewGroup extends FrameLayout {

    private static final int DEFAULT_CHILD_WIDTH_PX = 48;
    private static final int DEFAULT_CHILD_HEIGHT_PX = 48;

    public CustomViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = getResources().obtainAttributes(attrs, R.styleable.CustomViewGroup);
        int childWidthPx = a.getDimensionPixelSize(R.styleable.CustomViewGroup_itemWidth, DEFAULT_CHILD_WIDTH_PX);
        int childHeightPx = a.getDimensionPixelSize(R.styleable.CustomViewGroup_itemHeight, DEFAULT_CHILD_HEIGHT_PX);
        a.recycle();

        inflate(context, R.layout.custom_view_group, this);
        View child = findViewById(R.id.child);

        LayoutParams params = (LayoutParams) child.getLayoutParams();
        params.width = childWidthPx;
        params.height = childHeightPx;

        child.setLayoutParams(params);
    }
}

In order to support this, you have to declare styleable attributes for CustomViewGroup in res/values/attrs.xml :

<resources>
    <declare-styleable name="CustomViewGroup">
        <attr name="itemWidth" format="dimension"/>
        <attr name="itemHeight" format="dimension"/>
    </declare-styleable>
</resources>

And here's the layout I'm using for custom_view_group.xml . Note that there's no need to use a <merge> tag when you have only one child:

<View
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/child"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="#caf"/>

From there, you can use CustomViewGroup in any of your other layouts, and specify the size of the child view directly:

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.example.stackoverflow.CustomViewGroup
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:background="#eee"
        app:itemWidth="100dp"
        app:itemHeight="100dp"/>

</FrameLayout>

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM