小编sou*_*ris的帖子

将属性传递给复合视图中的子视图

我正在尝试创建一个复合视图,我可以在XML中设置属性并将它们传递给复合视图中的子项.在下面的代码中,我想设置android:text并将其传递给EditText.

这可以在不必将每个属性设置为自定义属性的情况下实现吗?

Activity.xml:

<FrameLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>
        <com.app.CustomLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="child_view_text" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

custom_view.xml:

<merge xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.design.widget.TextInputLayout>
</merge>
Run Code Online (Sandbox Code Playgroud)

CustomView.java:

public ValidationTextInputLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
}

private void init(Context context, AttributeSet attrs) {
    View v = inflate(context, R.layout.custom_view, this);

    mEditText = (EditText) v.findViewById(R.id.editText);
    mTextInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout);
}
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-support-library

14
推荐指数
1
解决办法
1153
查看次数