国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

基于Android LayoutInflater的使用介紹

2020-04-11 12:29:13
字體:
來源:轉載
供稿:網友

在android中,LayoutInflater有點類似于Activity的findViewById(id),不同的是LayoutInflater是用來找layout下的xml布局文件,并且實例化!而findViewById()是找具體xml下的具體 widget控件(如:Button,TextView等)。

下面通過一個例子進行詳細說明:

1、在res/layout文件夾下,添加一個xml文件dialog.xml

復制代碼 代碼如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal" >

<ImageView
  android:id="@+id/diaimage"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent" >
</ImageView>

<TextView
  android:id="@+id/diatv"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent" />

</LinearLayout>


2、在main.xml文件中添加一個按鈕,此按鈕用于實現點擊顯示一個Dialog
復制代碼 代碼如下:

<Button
android:id="@+id/btnshowdialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Dialog" />

3、在MainActivity的onCreate方法中添加如下代碼,實現具體功能操作
復制代碼 代碼如下:

  Button showdialog = (Button) findViewById(R.id.btnshowdialog);
  showdialog.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    AlertDialog dialog;
    LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.dialog, null);

    TextView diatv = (TextView) layout.findViewById(R.id.diatv);
    diatv.setText("Welcome to LayoutInflater study");
    ImageView image = (ImageView) layout.findViewById(R.id.diaimage);
    image.setImageResource(R.drawable.ic_launcher);

    builder.setView(layout);// <--important,設置對話框內容的View
    dialog = builder.create();
    dialog.show();
   }
  });


運行程序,點擊按鈕,將實現如下效果!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 乌拉特中旗| 吴桥县| 蒙城县| 伊金霍洛旗| 安陆市| 天祝| 信宜市| 治多县| 临江市| 保亭| 铜川市| 阜阳市| 尉氏县| 桓台县| 永丰县| 南城县| 黑龙江省| 黑山县| 灵台县| 双牌县| 峡江县| 南川市| 泽州县| 邵武市| 台山市| 攀枝花市| 喀喇沁旗| 章丘市| 巨鹿县| 凤台县| 彭阳县| 广灵县| 壤塘县| 通化市| 林口县| 桐梓县| 鄄城县| 疏勒县| 武义县| 武陟县| 大英县|