編輯:關於Android編程
首先對LayoutInflater下一個定義吧,Layoutinflater的作用就是將一個xml布局文件實例化為View對象。
獲取Layoutinflater對象的方法有三種,招不在多,管用就行,跟蹤源碼後發現三種方法的本質都是調用了context.getSystemService(),所以建議以後寫的時候就用context.getSystemService()。具體寫法如下:
[java]
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);接下來重點介紹inflate()方法的使用:
1、返回值:
View對象,有了View對象我們就能做諸如setContentView()或者在自定義Adapter裡面的getView()方法中獲取item的布局。
2、參數:
[java]
public View inflate (int resource, ViewGroup root)
public View inflate (int resource, ViewGroup root)
第一個參數是一個xml文件的R索引,比如R.layout.activity_main。第二個參數如果不為null就是將得到的View對象再放入一個Viewgroup容器中,我這麼說可能有的讀者還不是很清楚,文章的最後會有一個例子來說明問題。
[java]
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
這裡的第三個參數的意思是如果第二個參數不為null,那麼為true則放入第二個參數引用的Viewgroup中,為false則不放入第二個參數的Viewgroup中。
下面用一個實例來說明問題。
布局文件有兩個:
activity_main.xml
[html]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<RelativeLayout
android:id="@+id/viewgroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</LinearLayout>inflatertest.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflatedemo" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="inflatedemo" />
</LinearLayout>當Activity中如是寫:
[java]
<PRE class=java name="code">@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup);
}</PRE>
<PRE></PRE>
或者
<PRE></PRE>
[java]
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup); mInflater = (LayoutInflater) getApplicationContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE); mInflater.inflate(R.layout.inflatertest, mViewGroup); } @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup);
}
或者
[java]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, true);
}結果是這樣子的:
當Activity中如是寫:
[java]
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mViewGroup = (RelativeLayout) findViewById(R.id.viewgroup);
mInflater = (LayoutInflater) getApplicationContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.inflatertest, mViewGroup, false);
}結果是這樣的:
今天就到這裡,學習框架,一定要掌握函數的返回值和參數的意義,學會總結規律。
經過前面兩篇文章的學習,我們已經對ListView進行了非常深層次的剖析,不僅了解了ListView的源碼和它的工作原理,同時也將ListView中常見的一些
ViewPager引入示例首先讓大家有個全局的認識,直接上個項目,看看僅僅通過這幾行代碼,竟然就能完成如此強悍的功能。效果圖:實現了三個view間的相互滑動。第一個VIE
首先,讓我們確認下什麼是service? service就是android系統中的服務
本文章圍繞著Android的包管理機制,著重分析Android的包格式(包括簽名),以及應用程序的安裝,升級以及卸載過程。1. Android APK文件Android的