Android——动态加载布局

来源:互联网 发布:网络著名小说作家 编辑:程序博客网 时间:2024/05/22 00:33

动态加载其他布局的代码:

                        // 加载布局View show_note = View.inflate(Test_Note_Show.this,R.layout.note_show, null);// 设置动态布局的IDshow_note.setId(i);// 找到布局容器LinearLayout layout = (LinearLayout) findViewById(R.id.note_show_box);// 将布局加载到容器layout.addView(show_note);// 这里加入的Button是在delete_btn_box中另一个LinearLayout后面的View delete_btn = View.inflate(Test_Note_Show.this,R.layout.delete_btn, null);delete_btn.setId(10000 + i);LinearLayout layout_3 = (LinearLayout) findViewById(R.id.delete_btn_box);layout_3.addView(delete_btn);

note_show.xml:

<?xml version="1.0" encoding="UTF-8"?><LinearLayout android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_height="180dp" android:layout_width="match_parent"android:background="@drawable/note_frame_2"><LinearLayout android:id="@+id/delete_btn_box"android:layout_width="match_parent" android:layout_height="50dp"><!-- 就是在这个LinearLayout后面 --><LinearLayout android:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="8.8"></LinearLayout></LinearLayout><LinearLayout android:layout_width="match_parent"android:layout_height="120dp" android:orientation="horizontal"><Button android:id="@+id/show_btn" android:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="8.8"android:background="#00000000" /><Button android:id="@+id/edit_btn" android:layout_width="0dp"android:layout_height="match_parent" android:layout_weight="1.2"android:background="@drawable/feather_orange_opposite" /></LinearLayout></LinearLayout>

由此可见动态加载的顺序是首先加载静态布局,即.xml文件,然后再加载动态布局。



原创粉丝点击