Android動態添加和移除布局

來源:互聯網
上載者:User

標籤:布局   flat   rri   inf   err   new   cte   inflate   package   

 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)22     private LinearLayout mParentlayout;23 24     @ViewInject(R.id.add_view_button)25     private Button addViewButton;26     @ViewInject(R.id.remove_view_button)27     private Button removeViewButton;28 29     private LayoutInflater mLayountInflater;30     private LinearLayout linearlayout;31 32     private static int i=0;33     private Stack<View> mStack;34 35     @Override36     protected void onCreate(Bundle savedInstanceState) {37         super.onCreate(savedInstanceState);38         setContentView(R.layout.activity_dynamic_add_view);39         x.view().inject(this);40 41         mStack=new Stack<>();42         initView();43     }44 45     private void initView() {46         mLayountInflater=LayoutInflater.from(this);47         //mLayountInflater=getLayoutInflater();48         //mLayountInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);49 50         addViewButton.setOnClickListener(new InnerOnClickListener());51         removeViewButton.setOnClickListener(new InnerOnClickListener());52     }53 54     class InnerOnClickListener implements View.OnClickListener{55 56         @Override57         public void onClick(View v) {58             switch (v.getId()){59                 case R.id.add_view_button:60                     addView();61                     break;62                 case R.id.remove_view_button:63                     removeView();64                     break;65                 default:66                     break;67             }68         }69     }70 71     private void removeView() {72         if(mStack.size()>0){73             mParentlayout.removeView(mStack.pop());74         }75 76 77     }78 79     private void addView() {80         linearlayout= (LinearLayout) mLayountInflater.inflate(R.layout.add_view,null);81         TextView textView= (TextView) linearlayout.findViewById(R.id.add_text);82         i++;83         textView.setText("你添加的view"+i);84         mParentlayout.addView(linearlayout);85         mStack.push(linearlayout);86     }87 }

 

Android動態添加和移除布局

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.