【问题标题】:set child params from parent custom viewGroup从父自定义 viewGroup 设置子参数
【发布时间】:2018-02-06 14:14:44
【问题描述】:

我有一个带有 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>

我想像这样使用父级

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

如您所见,我想从父属性读取我的孩子的宽度和高度。有可能吗?
类似的东西,从父主题中读取,对吗?当您从父主题设置项目背景之类的值时。

 android:background="?attr/selectableItemBackground"

【问题讨论】:

    标签: android android-layout android-custom-view android-theme android-styles


    【解决方案1】:

    是的,这是可能的。以下是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);
        }
    }
    

    为了支持这一点,您必须在res/values/attrs.xml 中为CustomViewGroup 声明可样式属性:

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

    这是我用于custom_view_group.xml 的布局。请注意,如果您只有一个孩子,则无需使用&lt;merge&gt; 标签:

    <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"/>
    

    从那里,您可以在任何其他布局中使用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>
    

    【讨论】:

    • 谢谢你,本的回答。但我的意思是一种不涉及 java 代码的方法。
    • @max 你打算如何在没有任何 java 代码的情况下创建自定义视图子类?
    • 是的,我必须创建一个 java 类,但我不想再次管理 java.thanks 中的子属性。
    • @max 好的,很公平,但是如果不编写 java 来读取 attrs 并将它们应用于视图,就无法做你想做的事
    猜你喜欢
    • 2015-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    相关资源
    最近更新 更多