1.layout_gravity=start 時,從左向右 end時 從右向左
2.Inflater
在實際開發(fā)中LayoutInflater這個類還是非常有用的,它的作用類似于findViewById()。具體作用: 1、對于一個沒有被載入或者想要動態(tài)載入的界面,都需要使用LayoutInflater.inflate()來載入;
2、對于一個已經(jīng)載入的界面,就可以使用Activiyt.findViewById()方法來獲得其中的界面元素。
inflate 方法 通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象
/**
* LayoutInflater這個類的作用類似于findViewById(),
* 不同點:
* LayoutInflater是用來找layout下xml布局文件的,而且它會實例化
* findViewById()是找具體xml布局文件下的具體widget控件,比如:Button按鈕
*
*
*
* inflate就相當(dāng)于將一個xml中定義的布局找出來.
* 因為如果在一個Activity文件里直接用findViewById()這個方法的話,
* 那么它所對應(yīng)的是setConentView()中調(diào)用的那個layout里的組件.
* 因此如果在同樣的Activity里用到別的layout的話,
* 而且你還要設(shè)置這個layout里的組件(比如:ImageView,TextView)上的內(nèi)容,
* 那么你就必須用inflate()先將這個layout找出來, 然后再用這個layout對象去找到它上面的組件
* 然后進行一系列的操作
*
* inflate()方法中參數(shù):
* 1.想要用的布局文件的id
* 2.持有選項卡的內(nèi)容,獲取FrameLayout
* 3.true:將此處解析的xml文件做為根視圖View
*/
Inflate 方法
通過 sdk 的 api 文檔,可以知道該方法有以下幾種過載形式,返回值均是 View 對象,如下:
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很重要的一個API,它是Activity的一個方法,根據(jù)傳入的NAME來取得對應(yīng)的Object,然后轉(zhuǎn)換成相應(yīng)的服務(wù)對象。以下介紹系統(tǒng)相應(yīng)的服務(wù)。
傳入的Name 返回的對象 說明
WINDOW_SERVICE WindowManager 管理打開的窗口程序
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 手機震動的服務(wù)
CONNECTIVITY_SERVICE Connectivity 網(wǎng)絡(luò)連接的服務(wù)
WIFI_SERVICE WifiManager Wi-Fi服務(wù)
TELEPHONY_SERVICE TeleponyManager 電話服務(wù)
3.Bundle
兩個activity之間的通訊可以通過bundle類來實現(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(); //得到傳過來的bundle
String data = bundle.getString("Data");//讀出數(shù)據(jù)
setTitle(data);
}
}
4.Fragment
首先Fragment 就可以把它當(dāng)作一個view , 只不過這個view 與 activity一樣有了生命周期函數(shù)。在Activity中你可以通過getFragmentManager()來獲得Fragment對象,然后通過FragmentManager對象的beginFragmentTransaction()方法來獲得FragmentTransaction對象。通過它的add()方法來添加一個Fragment到當(dāng)前的Activity中。
一個FragmentTransaction對象可以執(zhí)行多個增刪修的方法,如果你想把這些修改提交到Activity上,必須在最后調(diào)用一下這個對象的commit()方法。
例子:http://www.2cto.com/kf/201407/318931.html