Android-以编程方式删除布局
我有一个活动,它使用for循环从共享首选项以编程方式生成布局。文本视图和按钮封闭在线性布局中。用户可以输入任意多个视图。现在,该按钮将是一个删除按钮。按下时,我想删除包含按钮和其他文本视图的线性布局。我该怎么做 这是我的密码:Android-以编程方式删除布局,android,android-layout,Android,Android Layout,我有一个活动,它使用for循环从共享首选项以编程方式生成布局。文本视图和按钮封闭在线性布局中。用户可以输入任意多个视图。现在,该按钮将是一个删除按钮。按下时,我想删除包含按钮和其他文本视图的线性布局。我该怎么做 这是我的密码: package com.dirkjan.myschools; import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; im
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;
}
}
package com.dirkjan.myschools;
导入android.app.Activity;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.content.res.Configuration;
导入android.graphics.Typeface;
导入android.os.Bundle;
导入android.view.Menu;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.TextView;
公共类MainActivity扩展了活动{
线性布局主体左、右;
按钮addsubc;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
subjectLeft=(LinearLayout)findViewById(R.id.llSubjectLeft);
subjectRight=(LinearLayout)findViewById(R.id.llSubjectRight);
//加载保存的主题
SharedReferences getSubjects=getSharedReferences(“主题信息优先”,模式为私有);
SharedReferences.Editor=getSubjects.edit();
int subjectCount=getSubjects.getInt(“count”,0);
如果(主题计数>0){
对于(int i=1;i,可以通过调用removeView(View View View)从父视图中删除子视图,例如:
parent.removeView(child);
如果要将其从布局中删除,可以调用view.setVisibility(view.GONE)
,如果只想隐藏它,可以调用view.setVisibility(view.INVISIBLE)
。假设您的LinearLayout ID是my\u linear\u布局,只需在onClickListener中执行以下操作:
findViewById(R.id.my_linear_layout).setVisibility(View.GONE);
在XML中,请确保输入ID:
<LinearLayout
android:id="@+id/my_linear_layout"
...>
</LinearLayout>
首先使用
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
final LinearLayout child = (LinearLayout) ll.findViewById(count);
使用
ll = (LinearLayout) findViewById(R.id.main_linearlayout);
final LinearLayout child = (LinearLayout) ll.findViewById(count);
现在,要删除整个布局,您可以使用removeview()
方法,如下所示
ll.removeView(child);
若要仅从特定布局中删除所有视图(此处为子视图),您可以使用
child.removeAllViews();
您可以像获取当前单击项目的id一样执行此操作
并在根布局中指定
LinearLayout layout = (LinearLayout) v.getParent();
并使用下面给出的代码删除:
linearLayout.removeView(layout);
我没有为要删除的子级分配id。由于这应该以编程方式完成,因此如何在不为每个布局分配id的情况下执行此操作?此处的计数是多少?使用ll.getChildCount()可以在局部布局中不获取任何子级
;没有为线性布局分配id,因为布局是通过编程方式添加的。在这种情况下,您可以直接执行ll.setVisibility(View.GONE)
应用程序如何知道ll是包含按钮的父布局?该方法将应用于线性布局及其所有子布局。我的意思是,有许多线性布局。在每个线性布局中,都有包含按钮的相对布局。布局上没有分配ID,视图和d按钮。如何设置onCLickListener()
?按钮位于相对布局中,该布局位于线性布局中,必须通过单击按钮删除线性布局
,因此您希望通过单击子视图删除父布局?那么该父布局的子视图呢?它们都将与父布局一起删除。是的,但如何删除在没有为所有内容分配id的情况下删除它。布局和视图是以编程方式创建的。这会隐藏视图,但不会将其从堆栈中删除。变量v
似乎没有出现在OP的问题中。是的,我正在举一个例子,v是onclick(视图v)中的视图。您可以使用“v”访问元素以获取它们的id