Android中在activity用程式碼動態添加布局
阿新 • • 發佈:2019-02-02
在activity中或fragment中動態添加布局
效果
專案結構
核心程式碼:
MainActivity
<span style="font-size:18px;">public class MainActivity extends Activity implements OnClickListener { private Button btn_jiazaibuju;//載入按鈕 private LinearLayout yuanbuju;//母佈局 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); btn_jiazaibuju.setOnClickListener(this); } /** * 載入佈局的按鈕點選事件 */ @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_jiazaibuju: setView(); break; } } /** * 載入佈局的方法 */ private ArrayList<View> views = new ArrayList<View>();//放view的list private void setView() { //移除其中的所有view yuanbuju.removeAllViews(); //給要載入的佈局設定引數 LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); params.leftMargin = 30; params.rightMargin = 30; params.topMargin = 30; //調inflate方法把佈局轉化成物件 View view1 = View.inflate(this, R.layout.jiedan_item, null); View view2 = View.inflate(this, R.layout.jiedan_item, null); View view3 = View.inflate(this, R.layout.jiedan_item, null); View view4 = View.inflate(this, R.layout.jiedan_item, null); View view5 = View.inflate(this, R.layout.jiedan_item, null); //把佈局物件放到list中 views.add(view1); views.add(view2); views.add(view3); views.add(view4); views.add(view5); //在增強for迴圈中載入所有佈局 for (View view:views) { yuanbuju.addView(view, 0, params); } } /** * 初始化原控制元件 */ private void init() { yuanbuju = (LinearLayout) findViewById(R.id.layout_taskfragment_container); btn_jiazaibuju = (Button) findViewById(R.id.btn_jiazaibuju); } }</span>
activity_main.xml
<span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_jiazaibuju" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="載入佈局按鈕" /> <ScrollView android:id="@+id/scrollView_taskfragment" android:layout_width="match_parent" android:layout_height="wrap_content" > <LinearLayout android:id="@+id/layout_taskfragment_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > </LinearLayout> </ScrollView> </LinearLayout> </span>
jiedan_item.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:padding="10dp" android:orientation="horizontal"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="6dp" android:gravity="center_vertical" android:orientation="vertical" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:id="@+id/text_jiedan_step" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="步驟 "/> <ImageView android:id="@+id/imageView_jiedan_number" android:layout_width="25dp" android:layout_height="25dp" android:layout_marginRight="5dp" android:src="#0033FF" /> <TextView android:id="@+id/text_jiedan_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:text="接單"/> </LinearLayout> <TextView android:id="@+id/text_jiedan_tipinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" android:layout_marginBottom="6dp" android:textSize="20sp" android:text="有新訂單,請聯絡客戶" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="6dp" > <TextView android:id="@+id/text_jiedan_phoneinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#666" android:text="客戶電話:" /> <TextView android:id="@+id/text_jiedan_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="15sp" android:textColor="#666" android:text="00000000000" /> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/imageView_jiedan_picture" android:layout_width="70dp" android:layout_height="70dp" android:layout_marginRight="6dp" android:src="#0033FF" /> </LinearLayout></span>
如果沒有明白下載我的例子
例子1動態載入
例子2動態載入,並分組
效果圖: