免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

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

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

聯(lián)系客服