【Android动态添加和移除布局】寸阳分阴须爱惜,休负春色与时光。这篇文章主要讲述Android动态添加和移除布局相关的知识,希望能为你提供帮助。
1 package com.hyang.administrator.studentproject; 2 3 import android.os.Bundle; 4 import android.support.v7.app.AppCompatActivity; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.widget.Button; 8 import android.widget.LinearLayout; 9 import android.widget.TextView; 10 11 import org.xutils.view.annotation.ViewInject; 12 import org.xutils.x; 13 14 import java.util.Stack; 15 16 /** 17* 动态添加布局 18*/ 19 public class DynamicAddViewActivity extends AppCompatActivity { 20 21@ViewInject(R.id.parent_layout) 22private LinearLayout mParentlayout; 23 24@ViewInject(R.id.add_view_button) 25private Button addViewButton; 26@ViewInject(R.id.remove_view_button) 27private Button removeViewButton; 28 29private LayoutInflater mLayountInflater; 30private LinearLayout linearlayout; 31 32private static int i=0; 33private Stack< View> mStack; 34 35@Override 36protected void onCreate(Bundle savedInstanceState) { 37super.onCreate(savedInstanceState); 38setContentView(R.layout.activity_dynamic_add_view); 39x.view().inject(this); 40 41mStack=new Stack< > (); 42initView(); 43} 44 45private void initView() { 46mLayountInflater=LayoutInflater.from(this); 47//mLayountInflater=getLayoutInflater(); 48//mLayountInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 49 50addViewButton.setOnClickListener(new InnerOnClickListener()); 51removeViewButton.setOnClickListener(new InnerOnClickListener()); 52} 53 54class InnerOnClickListener implements View.OnClickListener{ 55 56@Override 57public void onClick(View v) { 58switch (v.getId()){ 59case R.id.add_view_button: 60addView(); 61break; 62case R.id.remove_view_button: 63removeView(); 64break; 65default: 66break; 67} 68} 69} 70 71private void removeView() { 72if(mStack.size()> 0){ 73mParentlayout.removeView(mStack.pop()); 74} 75 76 77} 78 79private void addView() { 80linearlayout= (LinearLayout) mLayountInflater.inflate(R.layout.add_view,null); 81TextView textView= (TextView) linearlayout.findViewById(R.id.add_text); 82i++; 83textView.setText("你添加的view"+i); 84mParentlayout.addView(linearlayout); 85mStack.push(linearlayout); 86} 87 }
推荐阅读
- Android里使用正則表達式
- android.os.NetworkOnMainThreadException
- Android页面事件挂接模拟
- 原创源码角度分析Android的消息机制系列——ThreadLocal的工作过程
- android:Activity启动模式之singleTask
- Android五天乐(第三天)ListFragment与ViewPager
- Android硬件抽象层(HAL)深入剖析
- SharePoint创建Alternate Access Mapping (AAM)备用訪问映射
- 12+最佳免费动漫流媒体服务平台(无广告烦恼)