【发布时间】:2022-08-18 19:57:39
【问题描述】:
我在单击按钮时添加布局。
private void addLayout() {
layout2 = LayoutInflater.from(mContext).inflate(R.layout.product_layout, mLinearLayout, false);
productAuto = (AutoCompleteTextView) layout2.findViewById(R.id.tv_product);
qtyEditText = (EditText) layout2.findViewById(R.id.prod_qty);
prodPriceEditText = (EditText)layout2.findViewById(R.id.prod_price);
prodSpecsEditText = (EditText)layout2.findViewById(R.id.prod_specs);
removeProduct = (Button)layout2.findViewById(R.id.btn_rmv);
mLinearLayout.addView(layout2);
setProd();
this.initListenersPrd(getActivity());
}
private void initListenersPrd(final Context context) {
productAuto.setOnItemClickListener((parent, view, position, id) -> {
String newName = parent.getItemAtPosition(position).toString();
ProductDetail productDetail = mProductManager.getProductByPrdName(newName);
if (productDetail != null) {
this.prodPriceEditText.setText(productDetail.getPrice());
this.prodSpecsEditText.setText(productDetail.getProductSpec());
}
});
removeProduct.setOnClickListener(v -> {
if (mLinearLayout.getChildCount()>0)
{
((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);
}
});}
现在我想从应用程序中删除一个特定的产品表单,所以我在单击删除按钮时完成了((LinearLayout)mLinearLayout.getParent()).removeView(mLinearLayout);。但它会删除动态添加的整个布局。
图形用户界面
正如您在上图中看到的,我添加了 4-5 个产品布局,但是当我单击删除按钮时,我删除了所有布局。
更新 1
我在主布局中添加了以下布局,然后以编程方式在其中调用产品布局
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:id=\"@+id/ll_prod\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:orientation=\"vertical\" >
</LinearLayout>
产品布局是单独创建的,我在我的主布局中调用它。
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<!-- Copyright (C) 2014 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the \"License\");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an \"AS IS\" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:orientation=\"vertical\">
<LinearLayout
android:id=\"@+id/ll_out\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:background=\"@drawable/background_round\"
android:orientation=\"vertical\"
android:padding=\"5sp\">
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"match_parent\"
android:layout_marginTop=\"10sp\"
android:orientation=\"horizontal\">
<AutoCompleteTextView
android:id=\"@+id/tv_product\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_gravity=\"left|center_vertical\"
android:gravity=\"left\"
android:hint=\"Enter Product\"
android:inputType=\"text\" />
</LinearLayout>
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginTop=\"10sp\"
android:orientation=\"horizontal\">
<LinearLayout
android:layout_width=\"0dp\"
android:layout_height=\"wrap_content\"
android:layout_weight=\".5\"
android:orientation=\"vertical\">
<EditText
android:id=\"@+id/prod_qty\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:editable=\"false\"
android:focusable=\"true\"
android:focusableInTouchMode=\"true\"
android:hint=\"Enter Quantity\"
android:gravity=\"left\"
android:inputType=\"number\" />
</LinearLayout>
<LinearLayout
android:layout_width=\"0dp\"
android:layout_height=\"wrap_content\"
android:layout_weight=\".5\"
android:orientation=\"vertical\">
<EditText
android:id=\"@+id/prod_price\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:editable=\"false\"
android:focusable=\"false\"
android:focusableInTouchMode=\"false\"
android:hint=\"Prod Price\"
android:gravity=\"left\"
android:inputType=\"none\" />
</LinearLayout>
<LinearLayout
android:layout_width=\"0dp\"
android:layout_height=\"wrap_content\"
android:layout_weight=\".5\"
android:orientation=\"vertical\">
<EditText
android:id=\"@+id/prod_specs\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:editable=\"false\"
android:focusable=\"false\"
android:focusableInTouchMode=\"false\"
android:gravity=\"left\"
android:hint=\"Prod Specs\"
android:inputType=\"none\" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:layout_marginBottom=\"1dp\"
android:layout_marginTop=\"1dp\"
android:padding=\"0dp\">
<Button
android:id=\"@+id/btn_rmv\"
android:layout_width=\"match_parent\"
android:layout_height=\"wrap_content\"
android:text=\"Remove Product\"
android:textColor=\"@color/white\" />
</LinearLayout>
</LinearLayout>
每当我单击Add New Product Button 时,都会调用上述布局。
如何删除特定布局?
任何帮助将不胜感激。
标签: android android-linearlayout android-view