8

我有一个活动,它使用 for 循环从共享首选项以编程方式进行布局。文本视图和按钮包含在线性布局中。用户可以输入任意数量的视图。现在,该按钮将是一个删除按钮。按下时,我想删除包含按钮和其他文本视图的线性布局。我该怎么做呢?

这是我的代码:

package com.dirkjan.myschools;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

LinearLayout subjectLeft, subjectRight;

Button addSubj;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    subjectLeft = (LinearLayout) findViewById(R.id.llSubjectLeft);
    subjectRight = (LinearLayout) findViewById(R.id.llSubjectRight);

    //Load the saved subjects
    SharedPreferences getSubjects = getSharedPreferences("SubjectInfo_Prefs", MODE_PRIVATE);
    SharedPreferences.Editor editor = getSubjects.edit();

    int subjectCount = getSubjects.getInt("count", 0);
    if (subjectCount > 0 ){
        for (int i = 1; i <= subjectCount; i++){
            //Set the linear layout for each subject
            LinearLayout ll = new LinearLayout(this);
            ll.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams llParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            float scale = getResources().getDisplayMetrics().density;
            //SET BOTTOM MARGIN
            float margin = 5; //RESIZE MARGIN HERE!
            int margs = (int) (margin * scale + 0.5f);

            //SET PADDING IN DP
            float padding = 5; //RESIZE PADDING HERE!
            int pads = (int) (padding * scale +0.5f);
            llParams.setMargins(0,0,0,margs);

            //SETTING THE LINEARLAYOUT PARAMS
            ll.setLayoutParams(llParams);
            ll.setPadding(pads, pads, pads, pads);

            //SETTING THE BACKGROUND COLOR OF THE LINEAR LAYOUT
            String chosenColor = getSubjects.getString("chosenColor" + i, "BLUE");

            if (chosenColor.equals("Green")){
                ll.setBackgroundResource(R.color.HoloGreen);
            }else if (chosenColor.equals("Blue")){
                ll.setBackgroundResource(R.color.HoloBlue);
            }else if (chosenColor.equals("Gray")){
                ll.setBackgroundResource(R.color.HoloGray);
            }else if (chosenColor.equals("Orange")){
                ll.setBackgroundResource(R.color.HoloOrange);
            }else {
                ll.setBackgroundResource(R.color.HoloYellow);
            }

            //ADDING THE LAYOUT TO THE APPROPRIATE CONTAINER (LEFT OR RIGHT)
            if (i % 2 == 1){
                subjectLeft.addView(ll);
            } else {
                subjectRight.addView(ll);
            }

            //SETTING THE SUBJECT NAME TEXTVIEW
            TextView SubjectName = new TextView(this);
            SubjectName.setText(getSubjects.getString("subjectName" + i, "Error"));
            SubjectName.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            SubjectName.setTextSize(22);
            SubjectName.setTypeface(Typeface.DEFAULT_BOLD);

            //SETTING THE SUBJECT NUMB TEXT VIEW
            TextView SubjectNumber = new TextView(this);
            SubjectNumber.setText(getSubjects.getString("subjectNumb" + i, "Error"));
            SubjectNumber.setLayoutParams(new ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT));
            SubjectNumber.setTextSize(16);

            //Creating the divider line
            ImageView divider = new ImageView(this);
            LinearLayout.LayoutParams dividerParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 2);
            divider.setLayoutParams(dividerParams);
            divider.setBackgroundResource(R.color.Black);

            //Add Views into the Layout
            ll.addView(SubjectNumber);
            ll.addView(SubjectName);
            ll.addView(divider);
        }



    }

    addSubj = (Button) findViewById(R.id.buttonPlusSubject);
    addSubj.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent toAddSubj = new Intent(MainActivity.this,
                    AddSubjectActivity.class);
            startActivity(toAddSubj);
            finish();
        }
    });
}






@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

请注意,没有为每个布局分配 id。如果有代码来识别按钮的父级的父级,这将有所帮助(该按钮处于相对布局中,该布局处于线性布局中,必须通过单击按钮来删除线性布局。

4

5 回答 5

16

首先使用找到您的父布局

ll = (LinearLayout) findViewById(R.id.main_linearlayout);

使用获取子布局

final LinearLayout child = (LinearLayout) ll.findViewById(count);

现在要删除整个布局,您可以使用removeview()以下方法

ll.removeView(child);

仅从您可以使用的特定布局中删除所有视图(此处为例如子级)

child.removeAllViews();
于 2013-05-28T11:28:44.860 回答
14

view.setVisiblility(View.GONE)如果您想从布局中删除它,或者view.setVisibility(View.INVISIBLE)您只想隐藏它,您可以调用它。

于 2013-05-28T11:21:08.603 回答
5

您可以通过调用 removeView(View view) 从父视图中删除子视图,例如:

parent.removeView(child);
于 2013-05-28T11:20:32.153 回答
4

假设您的 LinearLayout ID 是 my_linear_layout,只需在您的 onClickListener 中执行此操作:

 findViewById(R.id.my_linear_layout).setVisibility(View.GONE);

在您的 XML 中,请务必输入 ID:

 <LinearLayout 
      android:id="@+id/my_linear_layout"
      ...>
 </LinearLayout>
于 2013-05-28T11:26:03.290 回答
0

您可以这样做,例如获取当前单击项目的 id 并在根布局中分配

LinearLayout layout = (LinearLayout) v.getParent();

并使用下面给出的代码删除:

linearLayout.removeView(layout);
于 2021-01-21T09:36:34.613 回答