我有一个活动,使用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。如果有一个代码来识别按钮父级的父级,则会有所帮助(该按钮位于相对布局中,该布局采用线性布局,必须通过单击按钮删除线性布局。
答案 0 :(得分:15)
如果您要将其从布局中删除,可以致电view.setVisiblility(View.GONE)
,如果您只想隐藏它,可以致电view.setVisibility(View.INVISIBLE)
。
答案 1 :(得分:15)
首先使用
找到您的父布局ll = (LinearLayout) findViewById(R.id.main_linearlayout);
使用
获取子布局final LinearLayout child = (LinearLayout) ll.findViewById(count);
现在要删除整个布局,您可以使用removeview()
方法,如下所示
ll.removeView(child);
仅删除特定布局中的所有视图(此处为例如.child),您可以使用
child.removeAllViews();
答案 2 :(得分: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>
答案 3 :(得分:3)
您可以通过调用removeView(视图视图)从父级中删除子视图,例如:
parent.removeView(child);
答案 4 :(得分:0)
你可以这样做,比如获取当前点击的项目的 id 并在根布局中分配
LinearLayout layout = (LinearLayout) v.getParent();
并使用下面给出的代码删除:
linearLayout.removeView(layout);