動態添加組件(XML)


1.利用LayoutInflater的inflate動態加載XML
mLinearLayout = (LinearLayout)findViewById(R.id.LinearLayout_ID);
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(resource--需要加載的XML, null);
XML:resource = R.layout.XML-Name;
mLinearLayout.removeAllViewsInLayout();移除當前LinearLayout的內容
mLinearLayout.addView(view);添加XML到LinearLayout

設置Width和Hight:
   LinearLayout.LayoutParams.FILL_PARENT、mLinearLayout.getLayoutParams().width等
另:
mLinearLayout.addView(v,
   new LinearLayout.LayoutParams(mLinearLayout.getLayoutParams().width,
   mLinearLayout.getLayoutParams().height));

2.利用View.inflate加載xml
Layout1.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
     xmlns:android="
http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
     android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

<TextView android:id="@+id/box_0"
   android:layout_width="fill_parent" android:layout_height="40px"
   android:background="#ff00ff00" android:text="This is main layout" />
<LinearLayout
   android:id="@+id/box_1" android:layout_width="fill_parent"
   android:layout_height="wrap_content" android:layout_below="@+id/box_0">
</LinearLayout>
</RelativeLayout>

Layout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:id="@+id/box" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="fill_parent" >

<TextView android:id="@+id/box_3" android:layout_width="fill_parent"
   android:layout_height="40px" android:background="#ffff0000"
   android:text="this is 2" />
</LinearLayout>

    setContentView(R.layout.layout_1);
    LinearLayout ll = (LinearLayout) findViewById(R.id.box_1);
    View vv = View.inflate(this, R.layout.layout_2, null);
    ll.addView(vv,      new LinearLayout.LayoutParams   ll.getLayoutParams ().width,ll.getLayoutParams().height));

這個主要是在某一個控件里面動態加載。

3.也可以利用inflate加載menu 首先在res/menu下創建自己的Menu XML—my_menu.xml:
<menu xmlns:android="
http://schemas.android.com/apk/res/android">
<item android:id="@+id/home"
    android:title="Home"
    android:icon="@drawable/home" >
</item>
<item android:id="@+id/help" android:title="Help"
    android:icon="@drawable/help" >
</item>
<item android:id="@+id/save" android:title="Save n Quit"
    android:icon="@drawable/save_quit" >
</item>
<item android:id="@+id/exit" android:title="Quit"
    android:icon="@drawable/exit" >
</item>
</menu>

public class Welcome extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.my_menu, menu);
    return true;
}

   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
     switch (item.getItemId()) {
     case R.id.home:
     // TODO
     break;
      case R.id.help:
      // TODO
     break;
     case R.id.save:
     // TODO
    break;
     case R.id.exit:
     // TODO
    break;
   }
   return true;
   }
}

4.兩種方法:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_sms,
   (ViewGroup) findViewById(R.id.toast_sms_root));


LayoutInflater mInflater = LayoutInflater.from(context);
View myView = mInflater.inflate(R.layout.customToast, null);
TextView sender = (TextView) myView.findViewById(R.id.sender);
TextView message = (TextView) myView.findViewById(R.id.message);


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com