zoukankan      html  css  js  c++  java
  • 转:LayoutInflater作用及使用

    作用: 
    1、对于一个没有被载入或者想要动态载入的界面, 都需要使用inflate来载入. 

    2、对于一个已经载入的Activity, 就可以使用实现了这个Activity的的findViewById()方法来获得其中的界面元素. 

    方法: 
       Android里面想要创建一个画面的时候, 初学一般都是新建一个类, 继承Activity基类, 然后在onCreate里面使用setContentView方法来载入一个在xml里定义好的界面. 

       其实在Activity里面就使用了LayoutInflater来载入界面, 通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以获得一个 LayoutInflater, 也可以通过LayoutInflater inflater = getLayoutInflater();来获得.然后使用inflate方法来载入layout的xml, 

     
    下面是一个简单的例子:
     
    首先我们要知道,什么是已经被载入的layout,什么是还没有载入的.我们启动一个应用,与入口Activity相关的layout{常见的是main.xml}就是被载入的,即在oncreate()方法中的.而其他的layout是没有被载入的.就要动态载入了或通过另一个activity.
     
    在实际开发种LayoutInflater这个类还是非常有用的,它的作用类似于 findViewById(),
    不同点是LayoutInflater是用来找layout下xml布局文件,并且实例化!而findViewById()是找具体xml下的具体 widget控件.
    为了让大家容易理解我做了一个简单的Demo,主布局main.xml里有一个Button,当点击Button,出现 Dialog,而这个Dialog的布局方式是我们在layout目录下定义的custom_dialog.xml文件(里面左右分布,左边 ImageView,右边TextView)。
    代码如下:
     1 import android.app.Activity;
     2 import android.app.AlertDialog;
     3 import android.content.Context;
     4 import android.os.Bundle;
     5 import android.view.LayoutInflater;
     6 import android.view.View;
     7 import android.view.View.OnClickListener;
     8 import android.widget.Button;
     9 import android.widget.ImageView;
    10 import android.widget.TextView;
    11 
    12 public class MainActivity extends Activity implements OnClickListener {
    13 
    14     private Button button;
    15 
    16     public void onCreate(Bundle savedInstanceState) {
    17         super.onCreate(savedInstanceState);
    18         setContentView(R.layout.main);
    19 
    20         button = (Button) findViewById(R.id.button);
    21         button.setOnClickListener(this);
    22     }
    23 
    24     @Override
    25     public void onClick(View v) {
    26 
    27     showCustomDialog();
    28     }
    29 
    30     public void showCustomDialog() {
    31         AlertDialog.Builder builder;
    32         AlertDialog alertDialog;
    33         Context mContext = MainActivity.this;
    34 
    35         LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
    36         View layout = inflater.inflate(R.layout.custom_dialog, null);
    37         TextView text = (TextView) layout.findViewById(R.id.text);
    38         text.setText("Hello, Welcome to Mr Wei's blog!");
    39         ImageView image = (ImageView) layout.findViewById(R.id.image);
    40         image.setImageResource(R.drawable.icon);
    41         builder = new AlertDialog.Builder(mContext);
    42         builder.setView(layout);
    43         alertDialog = builder.create();
    44         alertDialog.show();
    45     }
    46 }

     

  • 相关阅读:
    codeforces 869E. The Untended Antiquity(二维树状数组,随机化)
    bzoj 3083: 遥远的国度(树上换根操作,树剖+询问整个子树)
    hdu 5534 Partial Tree(dp+降唯,好题)
    AtCoder Regular Contest 075 E
    hihocoder 1387 A Research on "The Hundred Family Surnames"(树,lca,求同一颜色的直径)
    hdu 5458 Stability(生成树,树链剖分,好题)
    推荐一套个人ui组件库
    回望2019,期盼2020
    如何从产品的角度对待自己的博客
    致一名迷茫的我
  • 原文地址:https://www.cnblogs.com/liyiran/p/4570975.html
Copyright © 2011-2022 走看看