您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
(一)LayoutInflater简介
LayoutInflater最重要的功能就是将XML文件实例化为View对象,可以通过getSystemService(Context.LAYOUT_INFLATER_SERVICE)或getLayoutInflater()或的LayoutInflater对象,通过inflate方法来载入layout的xml。
(二)代码实现
MainActivity代码
package com.example.mydialog; import java.util.ArrayList; import java.util.HashMap; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.View; import android.widget.ImageView; import android.widget.TextView; public class MainActivity extends Activity { private ArrayList<HashMap<String, Object>> listItem; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); showCustomDialog(); } public void showCustomDialog() { AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = MainActivity.this; LayoutInflater inflater = (LayoutInflater) mContext .getSystemService(LAYOUT_INFLATER_SERVICE); View layout = inflater.inflate(R.layout.mydialog, null); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("LayoutInflater妙用"); ImageView p_w_picpath = (ImageView) layout.findViewById(R.id.p_w_picpath); p_w_picpath.setImageResource(R.drawable.albums); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); alertDialog.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } mydialog.xml文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:gravity="center_vertical" > <ImageView android:id="@+id/p_w_picpath" android:layout_width="38dp" android:layout_height="38dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/p_w_picpath" android:layout_centerVertical ="true" android:textColor="#000000" /> </RelativeLayout>
效果图
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。