如何通过 android 中的属性添加对另一个视图的引用?
How can you add a reference to another view through attributes in android?
我正在尝试创建一个自定义编辑文本,在用户输入时显示进度图标(如输入....)
我的布局是这样的:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/search_parent"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<core.controls.DelayedAutoCompleteTextView android:id="@+id/search"
android:layout_width="match_parent"
android:gravity="start"
android:maxLines="1"
android:lines="1"
android:singleLine="true"
android:textColor="#000"
android:layout_height="match_parent"
android:imeOptions="actionSearch"
android:textAppearance="?android:attr/textAppearanceMedium"
app:delay="1000"
app:progress="@+id/progress"/>
</android.support.design.widget.TextInputLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="gone"
android:layout_alignParentEnd="true"
android:id="@+id/progress"/>
</RelativeLayout>
我也在值文件夹中声明了这个样式
<declare-styleable name="dact">
<attr name="delay"
format="integer" />
<attr name="progress"
format="reference" />
</declare-styleable>
并且在视图的构造函数中,我已经使用 :
成功检索了进度条的 ID
int progress_id = a.getResourceId(Resource.Styleable.dact_progress, 0);
我的问题是,我怎样才能得到对实际进度条的引用,因为它甚至不在与编辑文本相同的视图组中?
提前感谢您提供的任何帮助
在 View#onAttachedToWindow
中,当您的视图附加到其父视图时,您可以遍历视图层次结构,尝试找到具有给定 ID 的最接近的所需视图,例如。 g.
ViewGroup parent = (ViewGroup) getParent();
View target;
while ((target = parent.findViewById(id)) == null) {
parent = (ViewGroup) parent.getParent();
}
如果没有这样的视图,这段代码会导致异常。
我正在尝试创建一个自定义编辑文本,在用户输入时显示进度图标(如输入....)
我的布局是这样的:
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="@+id/search_parent"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<core.controls.DelayedAutoCompleteTextView android:id="@+id/search"
android:layout_width="match_parent"
android:gravity="start"
android:maxLines="1"
android:lines="1"
android:singleLine="true"
android:textColor="#000"
android:layout_height="match_parent"
android:imeOptions="actionSearch"
android:textAppearance="?android:attr/textAppearanceMedium"
app:delay="1000"
app:progress="@+id/progress"/>
</android.support.design.widget.TextInputLayout>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:visibility="gone"
android:layout_alignParentEnd="true"
android:id="@+id/progress"/>
</RelativeLayout>
我也在值文件夹中声明了这个样式
<declare-styleable name="dact">
<attr name="delay"
format="integer" />
<attr name="progress"
format="reference" />
</declare-styleable>
并且在视图的构造函数中,我已经使用 :
成功检索了进度条的 ID int progress_id = a.getResourceId(Resource.Styleable.dact_progress, 0);
我的问题是,我怎样才能得到对实际进度条的引用,因为它甚至不在与编辑文本相同的视图组中?
提前感谢您提供的任何帮助
在 View#onAttachedToWindow
中,当您的视图附加到其父视图时,您可以遍历视图层次结构,尝试找到具有给定 ID 的最接近的所需视图,例如。 g.
ViewGroup parent = (ViewGroup) getParent();
View target;
while ((target = parent.findViewById(id)) == null) {
parent = (ViewGroup) parent.getParent();
}
如果没有这样的视图,这段代码会导致异常。