sou*_*ris 14 android android-custom-view android-support-library
我正在尝试创建一个复合视图,我可以在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)
我认为您可能可以将自定义文本(即 child_view_text)放入主题中,然后在父布局或视图上使用该主题。这样,所有子视图都将具有随自定义文本一起传入的 android:text 属性。
在你的情况下,它可能看起来像:
<string name="child_view_text">Child View Text</string>
<style name="CustomTheme" parent="Theme.AppCompat">
<item name="android:text">@string/child_view_text</item>
</style>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/CustomTheme"/>
<com.app.CustomLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Run Code Online (Sandbox Code Playgroud)