Android之LayoutInflater-创新互联

(一)LayoutInflater简介

创新互联致力于网站建设,网站制作设计,营销网页按需定制,成都外贸网站制作,企业网站建设,微信小程序开发,网站SEO优化,网站设计制作案例丰富,是成都做网站公司和建站公司,欢迎咨询。

  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>

效果图

Android之LayoutInflater

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。

网页名称:Android之LayoutInflater-创新互联
分享路径:https://www.cdcxhl.com/article10/ehddo.html

成都网站建设公司_创新互联,为您提供电子商务定制开发云服务器软件开发网站维护品牌网站设计

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都app开发公司