所以我有一个由线性布局组成的xml,其中包含一个Button和一个TextView,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical">
<Button
android:id="@+id/btnCell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="40dp"
android:text="Button"
android:textColor="@color/blueText" />
<View
android:height="wrap_content"
android:width="wrap_content"
android:background="@drawable/Test"/>
</LinearLayout>
我想在不同的xml中的其他布局中使用相同的布局。我每次都需要相同的按钮,因此我通过将其包含在两个布局中来重用它(两个布局都在相同的xml中,但是其中的一个是隐藏的):
第一
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include layout="@layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
第二个:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include layout="@layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
因此,我显示了第一个布局,并在应用程序开始时隐藏了第二个布局,随着用户在界面内移动,布局进行了交换,从而显示了另一个布局,第一个布局被隐藏了。
问题是我在java活动类中声明了Button,如下所示:
btnCell = (Button) thirdView.findViewById(R.id.btnCell);
btnCell.setOnClickListener(this);
并实现了监听器。
@Override
public void onClick(View v) {
if (v == btnCell) {
System.out.println("entered if");
}
System.out.println("entered function");
}
问题是,当我在显示第一个视图而第二个视图隐藏时单击按钮时,该按钮可以正常工作,但是当我取消隐藏第二个布局时,隐藏第一个布局并继续单击该按钮,那应该是与第一个相同,但布局不同,没有任何反应。我进行搜索后发现,发生这种情况的原因是,由于视图层次结构,仅将ID分配给了第一种布局中显示的按钮,而没有将其分配给第二种布局中的按钮。如何使两个按钮对同一动作做出反应,而不必在每个布局中声明一个新按钮,而是重新使用它?
我已经使用了这种类型的布局。您可以为两者创建不同的ID,并为该视图添加膨胀名称,并提供不同的名称,以便您可以区分两者。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include android:id="+id/firstOne" layout="@layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Android的第二个是
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/image"
/>
<include android:id="+id/secondTwo" layout="@layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句