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)
Xia*_*ang 37
我需要这个问题中描述的完全相同的功能.这是我的解决方案和源代码:https://github.com/laoyang/android-dynamic-views.您可以在此处观看视频演示:http://www.youtube.com/watch?v = 4HeqyG6FDhQ
基本上你会有两个xml布局文件:
TextEdit
,a Spinner
和一个ImageButton
用于删除.在Java代码中,您将使用inflate,addView,removeView等动态添加和删除行视图到容器中.在Android应用程序中,有一些可见性控制可以提供更好的用户体验.您需要为每行的EditText视图添加TextWatcher:当文本为空时,您需要隐藏添加新按钮和删除按钮.在我的代码中,我void inflateEditRow(String)
为所有逻辑编写了一个辅助函数.
android:animateLayoutChanges="true"
在xml中设置以启用动画主要活动的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)
这是我的一般方式:
View namebar = view.findViewById(R.id.namebar);
ViewGroup parent = (ViewGroup) namebar.getParent();
if (parent != null) {
parent.removeView(namebar);
}
Run Code Online (Sandbox Code Playgroud)
您好您可以通过添加相对布局尝试这种方式,然后添加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)
ViewGroup类在运行时为子视图管理提供 API,也允许添加/删除视图。
关于该主题的其他一些链接:
http://developer.android.com/reference/android/view/View.html
http://developer.android.com/reference/android/widget/LinearLayout.html
只需使用myView.setVisibility(View.GONE);
即可将其完全删除。但是如果你想保留其父级使用中占用的空间myView.setVisibility(View.INVISIBLE);
添加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)
归档时间: |
|
查看次数: |
222593 次 |
最近记录: |