基于Android LayoutInflater的使用介绍

2016-02-19 10:25 2 1 收藏

今天图老师小编给大家介绍下基于Android LayoutInflater的使用介绍,平时喜欢基于Android LayoutInflater的使用介绍的朋友赶紧收藏起来吧!记得点赞哦~

【 tulaoshi.com - 编程语言 】

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

下面通过一个例子进行详细说明:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

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" /

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

/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();
   }
  });

运行程序,点击按钮,将实现如下效果!

来源:https://www.tulaoshi.com/n/20160219/1594557.html

延伸阅读
Toast英文含义是吐司,在Android中,它就像烘烤机里做好的吐司弹出来,并持续一小段时间后慢慢消失 Toast也是一个容器,可以包含各种View,并承载着它们显示。 使用场景: 1、需要提示用户,但又不需要用户点击“确定”或者“取消”按钮。 2、不影响现有Activity运行的简单提示。 用法: 1、可以通过构造函数初始化: 代码如下: ...
在项目开发中,可能系统自带的一些widget不能满足我们的需求,这时就需要自定义View。 通过查看系统中的常用widget如Button,TextView,EditText,他们都继承自View,所以我们在继承自定义View的时候也自然的需要继承View。 1、首先新建一个类LView继承自View 代码如下: public class LView extends View {  private Paint paint; ...
MainActivity如下: 代码如下: package cn.testcallback; import android.os.Bundle; import android.widget.Toast; import android.app.Activity; /** * Demo描述: * Android中回调接口的使用 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(...
学习目的: 1、掌握在Android中如何建立CheckBox 2、掌握CheckBox的常用属性 3、掌握CheckBox选中状态变换的事件(监听器) CheckBox简介: CheckBox和Button一样,也是一种古老的控件,它的优点在于,不用用户去填写具体的信息,只需轻轻点击,缺点在于只有“是”和“否”两种情况,但我们往往利用它的这个特性,来获取用户的一...
标签: Web开发
HTML5的强大之一就是允许web程序申请一些临时或者永久的空间(Quota)在这里可以进行 数据的存储甚至文件的操作。 FileSystem提供了文件夹和文件的创建、移动、删除等操作,大大方便了数据的本地处理, 而且所有的数据都是在沙盒(sandboxed)中,不同的web程序不能互相访问,这就保证了数据 的完整和安全。 在CatWrite项目中,运用了HTML5的...

经验教程

491

收藏

64
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部