【发布时间】:2018-12-20 06:58:04
【问题描述】:
我正在从父视图膨胀视图。它膨胀得很好。视图添加了我想要的多次,但是当我想删除特定视图时,它不会删除该视图。 虽然该按钮在 toasts 上起作用。但在移除视图时不起作用。
这是我的主要活动:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/mainlayout"
android:orientation="vertical"
android:layout_height="wrap_content"
tools:context=".Activity.Reminders">
<android.support.v7.widget.Toolbar
android:id="@+id/remindertoolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reminders"
android:textSize="17sp"
android:layout_marginStart="20dp"/>
<ImageView
android:id="@+id/addreminder"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_marginStart="190dp"
android:layout_marginEnd="30dp"
android:src="@drawable/add"
tools:ignore="ContentDescription" />
</android.support.v7.widget.Toolbar>
<ImageView
android:id="@+id/imageView10"
android:layout_width="157dp"
android:layout_height="69dp"
android:layout_marginStart="230dp"
android:layout_marginTop="8dp"
app:srcCompat="@drawable/addreminder1" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="132dp"
android:layout_marginTop="134dp"
android:layout_marginEnd="123dp"
app:srcCompat="@drawable/alarmclock"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/textView68"
android:layout_width="wrap_content"
android:layout_height="28dp"
android:layout_marginStart="126dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="126dp"
android:text="@string/no_reminders_added_yet" />
这是我正在夸大的活动:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:background="@drawable/shape2"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/reminder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/reminder" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="200dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/custom_mesaage"
android:importantForAutofill="no"
android:inputType="text"
tools:targetApi="o" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="36sp" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/everyday" />
</LinearLayout>
<LinearLayout
android:id="@+id/buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="UselessParent">
<RadioButton
android:id="@+id/day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/day" />
<RadioButton
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/month" />
</RadioGroup>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/listofmode"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<android.support.v7.widget.RecyclerView
android:id="@+id/listofmode2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/deleteimage"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/delete"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/deletetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/delete" />
<ImageView
android:id="@+id/downimage"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginStart="220dp"
android:src="@drawable/downn"
tools:ignore="ContentDescription" />
<ImageView
android:id="@+id/upimage"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center"
android:layout_marginStart="20dp"
android:src="@drawable/upp"
tools:ignore="ContentDescription" />
</LinearLayout>
</LinearLayout>
我想添加要从父级中删除的视图,但它不是。我已在它显示的按钮单击中添加了一个 toast。
代码如下:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
@SuppressLint("InflateParams") final View rowView = inflater.inflate(R.layout.reminderaddition, null);
deleteme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.removeView((View) v.getParent());
Toast.makeText(getApplicationContext(), "this is a textview", Toast.LENGTH_LONG).show();
}
});
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
layout.removeView((View) v.getParent());
Toast.makeText(getApplicationContext(), "this is a button", Toast.LENGTH_LONG).show();
}
});
我正在使用这个tutorial:
【问题讨论】:
-
这一行的布局是什么 - layout.removeView(
-
贴出整个活动的代码以便更好地理解
-
LinearLayout 布局 = findViewById(R.id.mainlayout); @NehaK
-
不清楚您正在使用哪些
setOnClickListener()调用,但View v参数是被点击的View。假设这是 ID 为deleteimage的<Button>,v.getParent()将返回<LinearLayout>,它是<Button>的直接父级。但是,<LinearLayout>不是mainlayout的直接子代,所以什么也没有发生。您需要再上一层楼——即v.getParent().getParent()——但这并不是一个真正的好方法。 -
@MikeM。 setonclicklistner 都应该从父视图中删除子视图
标签: android android-studio layout-inflater