android中的 inflate是什么意思
inflate()的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1.获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2.关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)方法三个参数的含义
resource:需要加载布局文件的id,意思是需要将这个布局文件中加载到Activity中来操作。
root:需要附加到resource资源文件的根控件,什么意思呢,就是inflate()会返回一个View对象,如果第三个参数attachToRoot为true,就将这个root作为根对象返回,否则仅仅将这个root对象的LayoutParams属性附加到resource对象的根布局对象上,也就是布局文件resource的最外层的View上,比如是一个LinearLayout或者其它的Layout对象。
attachToRoot:是否将root附加到布局文件的根视图上
android 如何在 dialog 中添加图片
一般来说
在Android里显示Gif图片,只会显示第一帧。如果想显示gif图片的话可以用Movie来显示图片
。
在dialog显示图片可以使用AlertDialog
加载一个布局,布局中显示你所要显示的图片。比如
LayoutInflater
inflater
=
getLayoutInflater();
View
layout
=
inflater.inflate(R.layout.dialog,
(ViewGroup)
findViewById(R.id.dialog));
new
AlertDialog.Builder(this).setTitle(“自定义布局“).setView(layout)
.setPositiveButton(“确定“,
null)
.setNegativeButton(“取消“,
null).show();
希望能帮到你。
安卓如何获取layout中所有控件
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件代码块,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。 具体作用:
1、对于一个没有被载入或者想要动态载入的界面,都需要使用LayoutInflater.inflate()来载入;
2、对于一个已经载入的界面,就可以使用Activiyt.findViewById()方法来获得其中的界面元素。
LayoutInflater 是一个抽象类,在文档中如下声明:
public abstract class LayoutInflater extends Object
获得 LayoutInflater 实例的三种方式:
1.LayoutInflater inflater = getLayoutInflater(); //调用Activity的getLayoutInflater()
2.LayoutInflater localinflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
1. LayoutInflater inflater = LayoutInflater.from(context);
其实,这三种方式本质是相同的,从源码中可以看出:
getLayoutInflater():
Activity 的 getLayoutInflater() 方法是调用 PhoneWindow 的getLayoutInflater()方法,看一下该源代码:
public PhoneWindow(Context context) {
super(context);
mLayoutInflater = LayoutInflater.from(context);
}
可以看出它其实是调用 LayoutInflater.from(context)。
LayoutInflater.from(context):
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater ==null) {
throw new AssertionError(“LayoutInflater not found.“);
}
return LayoutInflater;
}
可以看出它其实调用 context.getSystemService()。
结论:所以这三种方式最终本质是都是调用的Context.getSystemService()。
inflate 方法 通过 sdk 的 api 文档,可以知道该方法有以下几种过载形式,返回值均是 View 对象,如下:
public View inflate (int resource, ViewGroup root);
3 public View inflate (XmlPullParser parser, ViewGroup root);
4 public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot);
5 public View inflate (int resource, ViewGroup root, boolean attachToRoot);
6
7 LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
8 View view = inflater.inflate(R.layout.custom, (ViewGroup)findViewById(R.id.test));
9 //EditText editText = (EditText)findViewById(R.id.content);
10 // error
EditText editText = (EditText)view.findViewById(R.id.content);
对于上面代码,指定了第二个参数 ViewGroup root,当然你也可以设置为 null 值。
注意:
·inflate方法与 findViewById 方法不同;
·inflater 是用来找 res/layout下的 xml 布局文件,并且实例化;
·findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等)。
android中的“inflate”是什么意思
inflate的作用就是将一个用xml定义的布局文件查找出来,注意与findViewById()的区别,inflate是加载一个布局文件,而findViewById则是从布局文件中查找一个控件。
1、获取LayoutInflater对象有三种方法
LayoutInflater inflater=LayoutInflater.from(this);
LayoutInflater inflater=getLayoutInflater();
LayoutInflater inflater=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
2、关于LayoutInflater类inflate(int resource, ViewGroup root, boolean attachToRoot)
如何利用Layoutinflator(布局扩展器)实例化单个列表项
[html] view plain copy
《?xml version=“1.0“
encoding=“utf-8“?》
《LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android“
android:orientation=“vertical“
android:layout_width=“fill_parent“
android:layout_height=“fill_parent“
》
《TextView
android:layout_width=“fill_parent“
android:layout_height=“wrap_content“
android:text=“@string/hello“
/》
《Button
android:id=“@+id/button“
android:layout_width=“wrap_content“
android:layout_height=“wrap_content“
android:text=“ShowCustomDialog“
/》
《/LinearLayout》
3.定义对话框的布局方式,我们在layout目录下,新建一个名为 custom_dialog.xml文件具体代码如下:
[html] view plain copy
《?xml version=“1.0“
encoding=“utf-8“?》
《LinearLayout
xmlns:android=“http://schemas.android.com/apk/res/android“
android:orientation=“horizontal“
android:layout_width=“fill_parent“
android:layout_height=“fill_parent“
android:padding=“10dp“
》
《ImageView android:id=“@+id/image“
android:layout_width=“wrap_content“
android:layout_height=“fill_parent“
android:layout_marginRight=“10dp“
/》
《TextView android:id=“@+id/text“
android:layout_width=“wrap_content“
android:layout_height=“fill_parent“
android:textColor=“#FFF“
/》
《/LinearLayout》
4.修改主程序LayouInflaterDemo.java代码如下:
[csharp] view plain copy
public class LayoutInflaterDemo extends Activity implements
OnClickListener {
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
showCustomDialog();
}
public void showCustomDialog()
{
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = LayoutInflaterDemo.this;
//下面俩种方法都可以
////LayoutInflater inflater = getLayoutInflater();
LayoutInflater inflater = (LayoutInflater)
mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(“Hello, Welcome to Mr Wei’s blog!“);
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
}
android LayoutInflater 获取不到控件
这个是获取不到的,因为这个textview没有实际被调用。但是编译时R文件中确实有这个textview的id值,所以编译什么的都不会报错。如果你需要获取其中的值,就在Intent中绑定数据。或者更改它显示内容的话,就在MainActivity中动态注册个广播接收器,从现在这个广播接收器接受广播,并改变TextView的显示内容。
android fragment和activity的区别
Fragment是到Android3.0+ 以后,Android新增了Fragments,在没有 Fragment 之前,一个屏幕只能放一个 Activity。这是一个起源时间大家要知道是什么时候开始引入的。
.Activity 代表了一个屏幕的主体,而Fragment可以作为Activity的一个组成元素。
一个Activity可以有若干个(0或n)Fragment构成。你可以把Fragment想象成Activity中的一个控件,只不过相对于一般控件,Fragment与Activity联系更为紧密,随着Activity的生命周期变化,Fragment也随之相应不同的生命周期函数。
Fragment 从功能上讲相当于一个子活动(Activity),它可以让多个活动放到同一个屏幕上,也就是对用户界面和功能的重用,因为对于大屏设备来说,纯粹的 Activity 有些力不从心。
Fragment 像是一个子活动,但是 Fragment 不是 Activity 的扩展,因为 Fragment 扩展自 android.app 中的 Object,而 Activity 是 Context 的子类。Fragment 有自己的视图层级结构,有自己的活动周期,还可以像活动一样响应后退按钮,Fragment 还有一个用作其初始化参数的包(Bundle),类似 Activity,Fragment 也可由系统自动保存并在以后还原。当系统还原 Fragment 时,它调用默认的构造函数(没有参数),然后将此Bundle还原到新创建的 Fragment 中,所以无论新建还是还原 Fragment,都要经过两个步骤:(1)调用默认构造函数(2)传入新的或者保存起来的Bundle。
一个Activity可以运行多个 Fragment,Fragment 切换时,由 FragmentTransaction 执行,切换时,上一个 Fragment 可以保存在后退栈中(Back Stack),这里的后退栈由 FragmentManager 来管理,注意 Fragment 和 Activity 的后退栈是有区别的:Activity 的后退栈由系统管理,而 Fragment 的后退栈由所在的Activity 管理。
Fragment不能脱离Activity而存在,只有Activity才能作为接收intent的载体。其实两者基本上是载体和组成元素的关系。
Fragment用来描述一些行为或一部分用户界面在一个Activity中,你可以合并多个fragment在一个单独的activity中建立多个UI面板,同时重用fragment在多个activity中.你可以认为fragment作为一个activity中的一节模块,fragment有自己的生命周期,接收自己的输入事件,你可以添加或移除从运行中的activity.一个fragment必须总是嵌入在一个activity中,同时fragment的生命周期受activity而影响,举个例子吧,当activity暂停,那么所有在这个activity的fragments将被destroy释放。然而当一个activity在运行比如resume时,你可以单独的操控每个fragment,比如添加或删除。不过因为Fragment和Activity的生命周期都比较复杂,我们分别对比下:创建一个fragment你必须创建一个Fragment的子类或存在的子类,比如类似下面的代码
public static class AndroidFragment extends Fragment{
@Override
public View onCreateView(LayoutInflaterinflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.android_fragment,container, false);
}
}
Fragment类的一些代码看起来有些像Activity为了让大家了解清楚,Android开发网给大家整理下 Fragment的生命周期大家可以参考一下网上关于生命周期的介绍 http://www.cnblogs.com/purediy/p/3276545.html,部分类似Activity的,我们详细解释
onCreate()
当fragment创建时被调用,你应该初始化一些实用的组件,比如在fragment暂停或停止时需要恢复的
onCreateView()
当系统调用fragment在首次绘制用户界面时,如果画一个UI在你的fragment你必须返回一个View当然了你可以返回null代表这个fragment没有UI.
那么如何添加一个Fragment到Activity中呢? Activity的布局可以这样写
《?xml version=“1.0“encoding=“utf-8“?》
《LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android“
android:orientation=“horizontal“
android:layout_width=“match_parent“
android:layout_height=“match_parent“》
《fragment android:name=“com.android.cwj.ArticleListFragment“
android:id=“@+id/list“
android:layout_weight=“1“
android:layout_width=“0dp“
android:layout_height=“match_parent“ /》
《fragment android:name=“com.android.cwj.ArticleReaderFragment“
android:id=“@+id/viewer“
android:layout_weight=“2“
android:layout_width=“0dp“
android:layout_height=“match_parent“ /》
《/LinearLayout》
android fragment里怎么写信息
import android.app.Dialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.drding.llsq.R;
/**
* 电话
*/
public class TelFragment extends Fragment {
private int pmwidth;//屏幕宽度
private SharedPreferences sp;
private Dialog dialog;
public static TelFragment getInstance(String text) {
TelFragment mIntance = new TelFragment();
return mIntance;
}
@SuppressWarnings(“unchecked“)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home_tel, null);
sp = getActivity().getSharedPreferences(“userInfo“, getActivity().MODE_PRIVATE);
pmwidth = getResources().getDisplayMetrics().widthPixels;
init(view);
onclik();
return view;
}
/** 找控件*/
private void init(View view){
View wuy = view.findViewById(R.id.wuy);
LinearLayout.LayoutParams params_adj = (LinearLayout.LayoutParams) wuy
.getLayoutParams();
params_adj.width = pmwidth;
wuy.setLayoutParams(params_adj);
// title_app = (TextView)view.findViewById(R.id.title_app);
// title_app.setText(R.string.title_business);
}
/**点击事件*/
private void onclik(){
// newws.setOnClickListener(Onclicklistener);
}
/**监听点击事件*/
View.OnClickListener Onclicklistener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final Intent it = new Intent();
switch (v.getId()) {
// case R.id.newws://消息
// break;
}
}
};
@Override
public void onDestroy() {
super.onDestroy();
}
}
创建listview的布局界面必须通过什么属性才能使数据显示在界面上
Android加载list列表数据主要通Adapter实现用显示列表控件: Listview GridView ExpandListview 显示具体数据需要通Adapter实现Android目前4种Adapter: ArrayAdapter SimpleAdapter SimpleCursorAdapter BaseAdapter ( 自定义Adapter) 具体操作步骤 ( 自定义Adapter例): xml定义Listview布局代码通ID找Listview控件构建Adapter象新建类继承自BaseAdapter重写四具体代码构造适配器设置Listviewadapter象新建适配器界面即显示数据数据变需要调用adapternotifyDataSetChanged即刷新界面 package com.beryl.gougou; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import java.util.List; /** * Created by yt on 16/11/14. */ public class MyAdapter extends BaseAdapter { private List datalist; private LayoutInflater inflater; public MyAdapter(Context context ,List datalist){ this.datalist = datalist; inflater = LayoutInflater.from(context); } @Override public int getCount() { return datalist.size(); } @Override public Object getItem(int position) { return datalist.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { //处参考网view缓存机制,示例demo说明 return null; } }
android开发中menuinflater是什么意思
我们知道,LayoutInflater是用来实例化整个布局文件,而 MenuInflater是用来实例化Menu目录下的Menu布局文件的。
传统意义上的菜单定义需要Override Activity的onCreateOptionsMenu,然后在里面调用Menu.add把Menu的一个个item加进来,比较复杂。而通过使用MenuInflater可以把Menu的构造直接放在Menu布局文件中,真正实现模型(Model)与视图(View)的分离,程序也看着清爽多了。