1.layout_gravity=start 時(shí),從左向右 end時(shí) 從右向左
2.Inflater
在實(shí)際開(kāi)發(fā)中LayoutInflater這個(gè)類還是非常有用的,它的作用類似于findViewById()。具體作用: 1、對(duì)于一個(gè)沒(méi)有被載入或者想要?jiǎng)討B(tài)載入的界面,都需要使用LayoutInflater.inflate()來(lái)載入;
2、對(duì)于一個(gè)已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來(lái)獲得其中的界面元素。
inflate 方法 通過(guò) sdk 的 api 文檔,可以知道該方法有以下幾種過(guò)載形式,返回值均是 View 對(duì)象
/**
* LayoutInflater這個(gè)類的作用類似于findViewById(),
* 不同點(diǎn):
* LayoutInflater是用來(lái)找layout下xml布局文件的,而且它會(huì)實(shí)例化
* findViewById()是找具體xml布局文件下的具體widget控件,比如:Button按鈕
*
*
*
* inflate就相當(dāng)于將一個(gè)xml中定義的布局找出來(lái).
* 因?yàn)槿绻谝粋€(gè)Activity文件里直接用findViewById()這個(gè)方法的話,
* 那么它所對(duì)應(yīng)的是setConentView()中調(diào)用的那個(gè)layout里的組件.
* 因此如果在同樣的Activity里用到別的layout的話,
* 而且你還要設(shè)置這個(gè)layout里的組件(比如:ImageView,TextView)上的內(nèi)容,
* 那么你就必須用inflate()先將這個(gè)layout找出來(lái), 然后再用這個(gè)layout對(duì)象去找到它上面的組件
* 然后進(jìn)行一系列的操作
*
* inflate()方法中參數(shù):
* 1.想要用的布局文件的id
* 2.持有選項(xiàng)卡的內(nèi)容,獲取FrameLayout
* 3.true:將此處解析的xml文件做為根視圖View
*/
Inflate 方法
通過(guò) sdk 的 api 文檔,可以知道該方法有以下幾種過(guò)載形式,返回值均是 View 對(duì)象,如下:
public View inflate (int resource, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root)
public View inflate (XmlPullParser parser, ViewGroup root, boolean attachToRoot)
public View inflate (int resource, ViewGroup root, boolean attachToRoot)
Java代碼
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;
}
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;
}
另外getSystemService()是Android很重要的一個(gè)API,它是Activity的一個(gè)方法,根據(jù)傳入的NAME來(lái)取得對(duì)應(yīng)的Object,然后轉(zhuǎn)換成相應(yīng)的服務(wù)對(duì)象。以下介紹系統(tǒng)相應(yīng)的服務(wù)。
傳入的Name 返回的對(duì)象 說(shuō)明
WINDOW_SERVICE WindowManager 管理打開(kāi)的窗口程序
LAYOUT_INFLATER_SERVICE LayoutInflater 取得xml里定義的view
ACTIVITY_SERVICE ActivityManager 管理應(yīng)用程序的系統(tǒng)狀態(tài)
POWER_SERVICEPowerManger 電源的服務(wù)
ALARM_SERVICE AlarmManager 鬧鐘的服務(wù)
NOTIFICATION_SERVICE NotificationManager 狀態(tài)欄的服務(wù)
KEYGUARD_SERVICE KeyguardManager 鍵盤鎖的服務(wù)
LOCATION_SERVICE LocationManager 位置的服務(wù),如GPS
SEARCH_SERVICE SearchManager 搜索的服務(wù)
VEBRATOR_SERVICE Vebrator 手機(jī)震動(dòng)的服務(wù)
CONNECTIVITY_SERVICE Connectivity 網(wǎng)絡(luò)連接的服務(wù)
WIFI_SERVICE WifiManager Wi-Fi服務(wù)
TELEPHONY_SERVICE TeleponyManager 電話服務(wù)
3.Bundle
兩個(gè)activity之間的通訊可以通過(guò)bundle類來(lái)實(shí)現(xiàn)類
1:TestBundle類:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestBundle extends Activity {
private Button button1;
private OnClickListener cl;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button1 = (Button) findViewById(R.id.button1);
cl = new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestBundle.this, Target.class);
Bundle mBundle = new Bundle();
mBundle.putString("Data", "data from TestBundle");//壓入數(shù)據(jù)
intent.putExtras(mBundle);
startActivity(intent);
}
};
類2: Target
import android.app.Activity;
import android.os.Bundle;
public class Target extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.target);
Bundle bundle = getIntent().getExtras(); //得到傳過(guò)來(lái)的bundle
String data = bundle.getString("Data");//讀出數(shù)據(jù)
setTitle(data);
}
}
4.Fragment
首先Fragment 就可以把它當(dāng)作一個(gè)view , 只不過(guò)這個(gè)view 與 activity一樣有了生命周期函數(shù)。在Activity中你可以通過(guò)getFragmentManager()來(lái)獲得Fragment對(duì)象,然后通過(guò)FragmentManager對(duì)象的beginFragmentTransaction()方法來(lái)獲得FragmentTransaction對(duì)象。通過(guò)它的add()方法來(lái)添加一個(gè)Fragment到當(dāng)前的Activity中。
一個(gè)FragmentTransaction對(duì)象可以執(zhí)行多個(gè)增刪修的方法,如果你想把這些修改提交到Activity上,必須在最后調(diào)用一下這個(gè)對(duì)象的commit()方法。
例子:http://www.2cto.com/kf/201407/318931.html