您的位置:首页 > 移动开发 > Android开发

Android中的LayoutInflater的运用

2015-07-25 20:46 477 查看
在Android中我们常常运用findViewById来寻找xml中的一个个控件(Button,TextView等等),但是我们能运用的只能是setContentView指向的单个xml中有的控件。当我们想要实现点击按钮运用另一个xml的布局的时候,此时我们还是运用findViewById的话,将会报错。于是,便诞生了LayoutInflater这个方法来实现这样得想法。话不多说,下面用一个简单的运用来实现。

MainActivity.java

package com.whisker.layoutinflater.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(buttonListener);
}

private OnClickListener buttonListener = new OnClickListener() {

@Override
public void onClick(View v) {
AlertDialog.Builder builder;
AlertDialog alertDialog;

LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, null);
TextView textView = (TextView) layout.findViewById(R.id.text);
textView.setText("Hello,Welcome to my blog!");
ImageView imageView = (ImageView) layout.findViewById(R.id.image);
imageView.setImageResource(R.drawable.ic_launcher);
builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
};
}


activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.whisker.layoutinflater.activity.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ShowCustomDialog"/>

</LinearLayout>


custom_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<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/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginRight="10dp"/>

<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>

</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: