动态添加和删除Android中的视图?

jon*_*ney 117 android android-view

如何TextView在Android应用程序中添加和删​​除s 等视图,例如在原始股票Android联系人屏幕上按下字段右侧的小图标,然后添加或删除由a TextView和a组成的字段editTextView(来自我可以看到).

关于如何实现这个的任何例子?

Tho*_*hle 208

从android-developers上的这个帖子来看,ViewParent和ViewGroup一般无法删除视图.您需要将父级转换为布局(如果它是布局)以实现您想要的.

例如:

View namebar = View.findViewById(R.id.namebar);
((ViewGroup) namebar.getParent()).removeView(namebar);
Run Code Online (Sandbox Code Playgroud)

  • ViewParent无法删除子视图,但ViewGroup可以。 (2认同)

Xia*_*ang 37

我需要这个问题中描述的完全相同的功能.这是我的解决方案和源代码:https://github.com/laoyang/android-dynamic-views.您可以在此处观看视频演示:http://www.youtube.com/watch?v = 4HeqyG6FDhQ

布局

基本上你会有两个xml布局文件:

  • 一个水平的LinearLayout 行视图,带有a TextEdit,a Spinner和一个ImageButton用于删除.
  • 一个垂直的LinearLayout 容器视图,只有一个Add new按钮.

控制

在Java代码中,您将使用inflate,addView,removeView等动态添加和删除行视图到容器中.在Android应用程序中,有一些可见性控制可以提供更好的用户体验.您需要为每行的EditText视图添加TextWatcher:当文本为空时,您需要隐藏添加新按钮和删除按钮.在我的代码中,我void inflateEditRow(String)为所有逻辑编写了一个辅助函数.

其他技巧

  • android:animateLayoutChanges="true"在xml中设置以启用动画
  • 使用自定义透明背景和按下的选择器,使按钮在视觉上与在线Android应用程序中的按钮相同.

源代码

主要活动的Java代码(这解释了所有逻辑,但是在xml布局文件中设置了很多属性,请参考Github源代码获取完整的解决方案):

public class MainActivity extends Activity {
// Parent view for all rows and the add button.
private LinearLayout mContainerView;
// The "Add new" button
private Button mAddButton;
// There always should be only one empty row, other empty rows will
// be removed.
private View mExclusiveEmptyView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row_container);
    mContainerView = (LinearLayout) findViewById(R.id.parentView);
    mAddButton = (Button) findViewById(R.id.btnAddNewItem);

    // Add some examples
    inflateEditRow("Xiaochao");
    inflateEditRow("Yang");
}

// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
    // Inflate a new row and hide the button self.
    inflateEditRow(null);
    v.setVisibility(View.GONE);
}

// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
    // remove the row by calling the getParent on button
    mContainerView.removeView((View) v.getParent());
}

// Helper for inflating a row
private void inflateEditRow(String name) {
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    final View rowView = inflater.inflate(R.layout.row, null);
    final ImageButton deleteButton = (ImageButton) rowView
            .findViewById(R.id.buttonDelete);
    final EditText editText = (EditText) rowView
            .findViewById(R.id.editText);
    if (name != null && !name.isEmpty()) {
        editText.setText(name);
    } else {
        mExclusiveEmptyView = rowView;
        deleteButton.setVisibility(View.INVISIBLE);
    }

    // A TextWatcher to control the visibility of the "Add new" button and
    // handle the exclusive empty view.
    editText.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {

            // Some visibility logic control here:
            if (s.toString().isEmpty()) {
                mAddButton.setVisibility(View.GONE);
                deleteButton.setVisibility(View.INVISIBLE);
                if (mExclusiveEmptyView != null
                        && mExclusiveEmptyView != rowView) {
                    mContainerView.removeView(mExclusiveEmptyView);
                }
                mExclusiveEmptyView = rowView;
            } else {
                if (mExclusiveEmptyView == rowView) {
                    mExclusiveEmptyView = null;
                }
                mAddButton.setVisibility(View.VISIBLE);
                deleteButton.setVisibility(View.VISIBLE);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
        }
    });

    // Inflate at the end of all rows but before the "Add new" button
    mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
Run Code Online (Sandbox Code Playgroud)


Cab*_*zas 9

这是我的一般方式:

View namebar = view.findViewById(R.id.namebar);
ViewGroup parent = (ViewGroup) namebar.getParent();
if (parent != null) {
    parent.removeView(namebar);
}
Run Code Online (Sandbox Code Playgroud)


kru*_*hah 6

您好您可以通过添加相对布局尝试这种方式,然后添加textview.

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
            (LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));

RelativeLayout relative = new RelativeLayout(getApplicationContext());
relative.setLayoutParams(lp);

TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);

EditText edittv = new EditText(getApplicationContext());
edittv.setLayoutParams(lp);

relative.addView(tv);
relative.addView(edittv);
Run Code Online (Sandbox Code Playgroud)

  • 有关如何从linearLayout中删除特定视图的任何示例? (9认同)

Soh*_*shi 5

只需使用myView.setVisibility(View.GONE);即可将其完全删除。但是如果你想保留其父级使用中占用的空间myView.setVisibility(View.INVISIBLE);

  • 它不是删除只是隐藏。 (5认同)

Gib*_*olt 5

Kotlin 扩展解决方案

添加removeSelf以直接调用视图。如果附加到父级,它将被删除。这使您的代码更具声明性,因此更具可读性。

myView.removeSelf()

fun View?.removeSelf() {
    this ?: return
    val parent = parent as? ViewGroup ?: return
    parent.removeView(this)
}
Run Code Online (Sandbox Code Playgroud)

以下是如何以编程方式将视图添加到ViewGroup.

// Built-in
myViewGroup.addView(myView)

// Reverse addition
myView.addTo(myViewGroup)

fun View?.addTo(parent: ViewGroup?) {
    this ?: return
    parent ?: return
    parent.addView(this)
}

// Null-safe extension
fun ViewGroup?.addView(view: View?) {
    this ?: return
    view ?: return
    addView(view)
}
Run Code Online (Sandbox Code Playgroud)