top*_*sde 5 android layout-inflater
我创建了一个应用程序,LinearLayout
每次按下按钮时我都会动态添加和删除textView .
我的问题是,当屏幕方向改变,重新启动活动时,添加的所有textViews都会消失.我不知道如何保持LinearLayout
膨胀状态.
这是我初始化视图和按钮的代码的一部分:
private LayoutInflater inflater;
private LinearLayout ll;
View view;
Button add;
Button delete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = getLayoutInflater();
ll = (LinearLayout)findViewById(R.id.ll);
add = (Button)findViewById(R.id.bAdd);
delete = (Button)findViewById(R.id.bDelete);
add.setOnClickListener(this);
delete.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)
在onClick
我添加或删除textViews的方法上:
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.bAdd:
{
view = inflater.inflate(R.layout.sublayout,ll,true);
break;
}
case R.id.bDelete:
{
int childSize = ll.getChildCount();
if(0 != childSize) {
ll.removeViewAt(childSize -1);
}
Log.i("InflateLayout", "childsize: " +childSize);
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
463 次 |
最近记录: |