精品伊人久久大香线蕉,开心久久婷婷综合中文字幕,杏田冲梨,人妻无码aⅴ不卡中文字幕

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
LayoutInflater作用及使用
1、對于一個沒有被載入或者想要動態載入的界面, 都需要使用inflate來載入.
2、對于一個已經載入的Activity, 就可以使用實現了這個Activiyt的的findViewById方法來獲得其中的界面元素.
方法:
Android里面想要創建一個畫面的時候, 初學一般都是新建一個類, 繼承Activity基類, 然后在onCreate里面使用setContentView方法來載入一個在xml里定義好的界面.
其實在Activity里面就使用了LayoutInflater來載入界面, 通過getSystemService(Context.LAYOUT_INFLATER_SERVICE)方法可以獲得一個 LayoutInflater, 也可以通過LayoutInflater inflater = getLayoutInflater();來獲得.然后使用inflate方法來載入layout的xml,
下面是一個簡單的例子:
首先我們要知道,什么是已經被載入的layout,什么是還沒有載入的.我們啟動一個應用,與入口Activity相關的layout{常見的是main.xml}就是被載入的,即在Oncreate()中的.而其他的layout是沒有被載入的.就要動態載入了或通過另一個activity.
在實際開發種LayoutInflater這個類還是非常有用的,它的作用類似于 findViewById(),
不同點是LayoutInflater是用來找layout下xml布局文件,并且實例化!而findViewById()是找具體xml下的具體 widget控件.
為了讓大家容易理解我[轉]做了一個簡單的Demo,主布局main.xml里有一個TextView和一個Button,當點擊Button,出現 Dialog,而這個Dialog的布局方式是我們在layout目錄下定義的custom_dialog.xml文件(里面左右分布,左邊 ImageView,右邊TextView)。
代碼如下:
package com.bivin;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
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 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 = MainActivity.this;
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();
}
}
LayoutInflater的用法有三種:
第一種方法:
[java] view plaincopy
LayoutInflater inflater = LayoutInflater.from(this);
View layout = inflater.inflate(R.layout.main, null);
第二種方法:
[java] view plaincopy
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.main, null);
第三種方法:
[java] view plaincopy
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.main, null);
本站僅提供存儲服務,所有內容均由用戶發布,如發現有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
【android基礎學習之五】——基礎控件Menu,Dialog,ImageView,ImageButton
setContentView和inflate區別
Android ListView設置weight無效
Android自定義UI陷阱:LayoutInflater.from().inflate()一定不能工作在父類或虛類里
Android中UI設計的一些技巧?。?!
Android 仿QQ主頁面的實
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯系客服!

聯系客服

主站蜘蛛池模板: 莆田市| 怀宁县| 潜江市| 额济纳旗| 庆元县| 策勒县| 靖江市| 同德县| 玉龙| 盱眙县| 湛江市| 通江县| 富川| 松原市| 铜鼓县| 贵州省| 岑巩县| 米林县| 玉树县| 田东县| 东安县| 十堰市| 罗源县| 西城区| 三原县| 德格县| 嘉鱼县| 峡江县| 南雄市| 崇义县| 海城市| 油尖旺区| 改则县| 化州市| 霍林郭勒市| 抚顺市| 会泽县| 江西省| 香河县| 康定县| 东丰县|