[英]set child params from parent custom viewGroup
I have a CustomViewGroup with xml like this: 我有一个这样的xml的CustomViewGroup:
<?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: 这是CustomViewGroup
的必要Java代码...确实还不错:
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
: 为了支持这一点,您必须在res/values/attrs.xml
为CustomViewGroup
声明可样式化的属性:
<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
. 这是我用于custom_view_group.xml
的布局。 Note that there's no need to use a <merge>
tag when you have only one child: 请注意,只有一个孩子时,无需使用<merge>
标记:
<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: 从那里,您可以在任何其他布局中使用CustomViewGroup
,并直接指定子视图的大小:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.