我使用布局inflater,用一些按钮和一些文本视图来扩展线性布局。如何在膨胀的布局中访问小部件?
例如:我有一个按钮,我想为它设置onclick监听器。通常用“findViewById”声明按钮,然后设置监听器。但是膨胀按钮不存在于活动xml中(线性布局使用不同的xml而不是活动xml)以使用'findViewById'查找其视图。
我的inflater代码:
LinearLayout item = (LinearLayout )findViewById(R.id.linear_layout_duel_basic_easy);
View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);
item.addView(child_1);
我要隐藏的布局:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
感谢您的帮助。
答案 0 :(得分:5)
它应该工作。这里没什么特别的。
django.core.exceptions.ImproperlyConfigured: Module "core_app.forms" does
not define a "SignupForm" class
答案 1 :(得分:1)
根据评论:
在活动xml中,我有一个垂直线性布局(为 滚动视图)和我用来充气的xml有一个水平 按钮和textview
的线性布局
以下是如何实现它。
LinearLayout item = (LinearLayout)findViewById(R.id.linear_layout_duel_basic_easy); // Your vertical linearLayout
View child_1 = getLayoutInflater().inflate(R.layout.linear_layout_duel_1, null);// Your Horizontal LinearLayout
item.addView(child_1);
要访问水平LinearLayout内的视图: -
TextView tv = child_1.findViewById(R.id.textView);
Button btn = child_1.findViewById(R.id.button);
答案 2 :(得分:0)
您是否尝试覆盖onFinishInflate()
?
在里面你可以查找充气按钮,你可以简单地迭代你所有的孩子来找到你需要的视图。
//iterate children
for (int index = 0; index < getChildCount(); index+=1){
getChildAt(index);
//check if that view is what you need
}