首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

LayoutInflater的应用

2012-09-21 
LayoutInflater的使用//通后对话框显示一个视图public void showCustomDialog(){AlertDialog.Builder buil

LayoutInflater的使用
//通后对话框显示一个视图 public void showCustomDialog() { AlertDialog.Builder builder; AlertDialog alertDialog; Context mContext = LayoutInflaterDemo.this; //下面俩种方法都可以 //LayoutInflater inflater = getLayoutInflater(); LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE); View view= inflater.inflate(R.layout.custom_dialog,null); //R.layout.custom_dialog为XML的布局文件 TextView text = (TextView) view.findViewById(R.id.text); text.setText("Hello!"); ImageView image = (ImageView) view.findViewById(R.id.image); image.setImageResource(R.drawable.icon); builder = new AlertDialog.Builder(mContext); builder.setView(layout); alertDialog = builder.create(); alertDialog.show(); } //通过Toast来显示视图protected void showToast(int type){Toast.makeText(this, "*********", Toast.LENGTH_LONG).show(); LayoutInflater inflater = LayoutInflater.from(this); View view=inflater.inflate(R.layout.ID, null); //2句可以合成一句//View view=LayoutInflater.from(this).inflate(R.layout.ID, null); Toast toast = new Toast(this); toast.setView(view); toast.setDuration(type); toast.show(); } ?
?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;}

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayout android:id="@+id/placeslist_linearlayout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"></LinearLayout></ScrollView>

不错,学习了。 3 楼 passwrod 2012-08-02   marke  以后有时间看

热点排行