1.dialog對話框:java
A.警告對話框:AlertDialog,能夠有0到3個按鈕,一個單選框或複選框列表的對話框,警告對話框可建立大多數的交互界面,是推薦的類型android
B.進度對話框ProgressDialog:顯示一個進度環或進度條,因爲它是AlertDialog的擴展,因此也支持按鈕spring
C.日期選擇對話框DatePickerDialog:讓用戶選擇一個日期json
D.時間選擇對話框TimePickerDialog:讓用戶選擇一個時間緩存
E.若是你但願自定義對話框,能夠擴展服務器
2.建立對話框:網絡
A.普通的告警對話框:多線程
建立AlertDialog的方式:app
AlertDialog.Builder builder=new AlertDialog.Builder(this);框架
//設置警告信息,設置對話框不可取消(不能用back鍵取消)
builder.setMessage(「你肯定退出嗎」).setCancelable(false)
//設置警告按鈕
.setPositiveButton(「肯定」,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
MyActivity.this.finish();
}
}
.setNegativeButton(「取消」,new AlertDialog.OnClickListener(){
public void onClick(DialogInterface dialog,int id){
Dialog.cancel();
}
});
AlertDialog alert=builder.create();
alert.show();//顯示對話框
B.建立具備可選項的AlertDialog對話框:
final CharSequence[] items={「紅」,」綠」,」藍」};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle(「請選擇一種顏色:」);
builder.setItems(items,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int item){
Toast.makeText(getApplicationContext(),items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert=builder.create();
alert.show();//顯示對話框
C.建立單選框和多選框
final CharSequence[] items={「紅」,」綠」,」藍」};
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle(「請選擇一種顏色:」);
builder.setSingleChoiceItems(items,-1,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int item){
Toast.makeText(getApplicationContext(),items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert=builder.create();
alert.show();//顯示對話框
第二個參數表示默認選中的選項位置,使用-1表示默認狀況下不選中任何選項
建立多選列表使用setMultiChoiceItems()方法
D.建立進度條對話框
ProgressDialog progressDialog;
progressDialog=new ProgressDialog(mcontext);
//設置進度條顯示樣式
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//設置進度條顯示內容
progressDialog.setMessage(「Loading...」);
//進度條是否可取消
progressDialog.setCancelable(false);
E.信息內容是一個簡單的view類型
new AlertDialog.Builder(this).setTitle(「請輸入」)
.setIcon(R.drawable.ic_dialog_info)
.setView(new EditText(this))
.setPositiveButton(「肯定」,null)
.setNegativeButton(「取消」,null).show();
F.信息內容是一組單選框
new AlertDialog.Builder(this).setTitle(「單選框」)
.setIcon(R.drawable.ic_dialog_info)
.setSingleChoiceItems(new String[]{「item1」,」item2」},0,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
dialog.dismiss();
}
}).setNegativeButton(「取消」,null).show();
G.信息內容是一組複選框
new AlertDialog.Builder(this).setTitle(「複選框」)
.setMultiChoiceItems(new String[]{「item1」,「item2」},null,null)
.setPositiveButton(「肯定」,null)
.setNegativeButton(「取消」,null).show();
H.信息內容是一組簡單的列表
new AlertDialog.Builder(this).setTitle(「列表框」)
.setItems(new String[]{「item1」,」item2」})
.setPositiveButton(「肯定」,null).show();
I.信息內容爲一個自定義的佈局文件
LayoutInflater inflater=getLayoutInflater();
View view=inflater.inflate(R.layout.dialog,null);
new AlertDialog.Builder(this).setTitle(「自定義佈局」)
.setPositiveButton(「肯定」,null)
.setNegaviteButton(「取消」,null).show();
3.五大布局:
A.線性佈局LinearLayout,線性佈局以設置的垂直或水平屬性值,來排列全部的子元素。全部的子元素都會排列在其餘元素以後,所以一個垂直列表的每一行只有一個元素,而一個水平列表只有一個行高,
線性佈局保持子元素之間的間隔以及互相對齊。線性佈局還支持爲單獨的子元素設置weight,好處是容許子元素填充屏幕的剩餘空間,子元素指定weight值,剩餘的空間就會按照子元素的weight比例分配給子元素,weight用法概括:按比例劃分水平方向,將涉及到的view的android:width設置爲0dp,而後設置android:
Weight爲相應的比例便可;豎直方向,只需設置android:height爲0dp,以後設置weight屬性便可。
ViewGroup繼承自View,AbsoluteLayout,LinearLayout,FrameLayout,RelativeLayout繼承自ViewGroup,TableLayout繼承自LinearLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<EditText android:text="EditText"
android:id="@+id/editText1"
android:gravity=」center」
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<LinearLayout android:id="@+id/linearLayout1"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="right">
<Button android:id=@+id/btn
Android:text=」button」/>
</LinearLayout>
</LinearLayout>
android:gravity屬性設置管理器內組件的對齊方式,該屬性支持top,bottom,left,right,center_vertical,fill_vertical,center_horizontal,
fill_horizontal,center,fill,
clip_horizontal,clip_vertical幾個屬性,也能夠同時指定多重對齊方式的組合,如left|center_vertical表明出如今屏幕左邊,並且垂直居中
B.表格佈局TableLayout,表格佈局將子元素的位置分配到行或列中,一個TableLayout由多個TableRow組成,每個TableRow都會有一個row。表格佈局不會顯示row,columns和cell的邊框線。表格容許單元格爲空,單元格不能跨列,若是直接向TableLayout添加組件,那麼該組件將直接佔一行
a. 代碼示例:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=」fill_parent」
android:layout_height=」fill_parent」
android:stretchColumns=」1」>
<TableRow>
<TextView android:layout_column=」1」
android:padding=」3dip」
Android:text=」1row」/>
</TableRow>
<TableRow>
<TextView android:layout_column=」1」
android:padding=」3dip」
Android:text=」2row」/>
</TableRow>
</TableLayout>
b. 其餘xml屬性:
android:collapseColumns:設置須要被隱藏的列序號,多個序列號之間用逗號隔開
android:shrinkColumns:設置容許被收縮的列的列序號,多個序列號之間用逗號隔開
android:stretchColumns:設置容許被拉伸的列的列序號,多個序列號之間用逗號隔開
C.相對佈局RelativeLayout,相對佈局容許子元素指定它們相對於其餘元素或父元素的位置(經過ID指定)。所以,你能夠右對齊,或上下或置於屏幕的中央的形式來排列元素。元素按順序排列,所以若是第一個元素位於屏幕的中央,那麼相對這個元素的其餘元素將以屏幕中央的相對位置來排列。
a.代碼示例:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width=」fill_parent」
android:layout_height=」fill_parent」
<TextView android:id=」@+id/label」
android:layout_width=」fill_parent」
android:layout_height=」wrap_content」
Android:text=」歡迎訪問個人博客」/>
<EditText android:id=」@+id/entry」
android:layout_width=」fill_parent」
android:layout_height=」wrap_content」
android:layout_below=」@id/label」/>
<Button android:id=」@+id/btn」
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:layout_below=」@id/label」
android:layout_alignParentRight=」true」
android:layout_marginLeft=」10dip」
android:text=」OK」/>
<Button android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:layout_toLeftOf=」@id/btn」
android:layout_alignTop=」@id/btn」
android:text=」取消」/>
</RelativeLayout>
b.android:layout_toRightOf:控制該子組件位於給出id組件的右側, android:layout_toRightOf: 控制該子組件位於給出id組件的左側
android:layout_above: 控制該子組件位於給出id組件的上方;android:layout_below: 控制該子組件位於給出id組件的下方
android:layout_alignTop: 控制該子組件位於給出id組件的上邊界對齊;android:layout_alignBottom: 控制該子組件位於給出id組件的下邊界對齊
android:layout_alignLeft: 控制該子組件位於給出id組件的左邊界對齊;android:layout_alignRight: 控制該子組件位於給出id組件的右邊界對齊
值均爲給出組件的id:android:layout_alignLeft=」@id/view」
D.絕對佈局AbsoluteLayout,絕對佈局可讓子元素指定準確的x,y座標並顯示在屏幕上。(0,0)爲左上角,當向下或向右移動時座標值變大。絕對佈局沒有頁邊框,容許元素之間相互重疊(儘管不推薦)。
E.框架佈局FrameLayout,框架佈局被定製爲屏幕上的一個空白備用區域,以後你能夠在其中填充一個單一對象。全部的子元素都會固定在屏幕的左上角,不能爲框架佈局的一個子元素指定位置,後一個子元素將會直接在前一個子元素上進行填充覆蓋,把它們部分或所有擋住。
4.活動之間的跳轉:
A.startActivity(Intent):只是從當前界面跳到另一個界面,兩個界面再也不有聯繫
B.startActivityForResult(intent):能夠經當前界面認爲當前界面爲一個父窗體,要跳轉的界面爲子窗體,當子窗體關閉時,父窗體會執行onActivityResult()方法,並能夠得到子窗體的返回值
C.代碼:
Intent intent=new Intent();
//傳遞參數
intent.putExtra(「myText」,」你好,第二個活動」);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
SecondActivity中獲取參數代碼:
//從MainActivity跳轉到SecondActivity利用Intent傳遞參數
Intent intent=getIntent();
String info=intent.getStringExtra(「myText」);
注:SecondActivity需在activityMainfest當中進行註冊
使用Bundle傳遞參數:
Intent intent=new Intent();
Bundle bundle=new Bundle();
bundle.putString(「info」,」你好」);
intent.putExtras(bundle);
intent.setClass(MainActivity.this,SecondActivity.class);
startActivity(intent);
SecondActivity中獲取參數代碼:
Bundle bundle=this.getIntent().getExtras();
String text=bundle.getString(「info」);
5.android使用http協議與服務器通訊:
A.android目前只提供兩種http 通訊方式,HttpURLConnection和HttpClient,HttpURLConnection多用於接收和發送流式數據,所以比較適合上傳和下載文件
B.HttpClient通訊流程:
a建立HttpClient對象,該對象可用來屢次發送不一樣的http請求
b.建立HttpPost或HttpGet對象,設置參數,每發送一次http請求,都須要這樣一個對象
c.利用HttpClient的execute方法發送請求並等待結果,該方法會一直阻塞當前線程,直到返回結果或拋出異常
d.針對結果或異常作相應處理
C.需考慮的問題:
a.HttpClient對象能夠重複使用,能夠做爲類的靜態變量
b.HttpGet和HttpPost通常沒法重複使用,可建立一個方法用來初始化,同時設置一些須要上傳到服務器的資源
c.目前android再也不支持在UI線程中發起http請求,這樣會阻塞UI線程,所以還須要一個子線程發送http請求,執行execute方法
d.不一樣的請求對應不一樣的返回結果,對於如何處理結果(通常都是json或UI更新),須要必定的自由度
e.簡單來講,每次須要發送http 請求時,開一個子線程用於發送請求,子線程接收到結果或拋出異常時,根據狀況向UI線程發送message,最後在UI線程的handler或handleMessage方法中作結果解析和UI更新
D.代碼示例:
public class MainActivity extends Activity{
//模擬器把本身當成localhost,服務器應爲10.0.2.2
private static String url=」http://10.0.2.2:8080/PDAServer/login.action」;
private static Handler handler=new Handler();
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getPDAServiceData(url);
}
//請求服務
private void getPDAServiceData(String url){
url+=」?username=123456」;
HttpClient client=new DefaultHttpClient();
HttpPost request;
//如下紅色字體需處理異常
request=new HttpPost(new URL(url));
HttpReponse response=client.execute(request);
//判斷請求是否成功
if(reponse.getStatusLine().getStatusCode==200){
HttpEntity entity=reponse.getEntity();
if(entity!=null){
String out=EntityUtils.toString(entity);
//將從服務器獲取的數據展現出來,使用多線程的方式
handler.post(new Runnable(){
public void run(){
new AlertDialog.Builder(this).setMessage(out).create().show();
}
});
}
}
}
}
6.Handler多線程處理
A.Handler,直接繼承自Object,它容許發送和處理message或Runnable對象,而且會關聯到主線程MessageQueue中,每一個handler就是一個單獨的線程,而且關聯到消息隊列的線程。
當實例化一個handler時,它就承載着一個線程和一個消息隊列的線程,這個線程就能夠把message或Runnable壓入到消息隊列,而且從消息隊列中取出message或Runnable,進而操做他們
B.Handler主要有兩個做用:在工做線程中發送消息;在UI線程中獲取、處理消息
C.Handler壓入消息隊列有兩大致系:post和sendMessage
a.Post:Post容許把一個Runnable對象壓入到消息隊列中。它的方法有:post(Runnable),postAtTime(Runnable,long),postDelayed(Runnable,long)
b.sendMessage:sendMessage容許把一個包含消息數據的Message對象壓入到消息隊列中,它的方法有:
sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message,long),sendMessageDelayed(Message,long)
D.Post:對於handler的post方式來講,它會傳遞一個Runnable對象到消息隊列中,在Runnable對象中重寫run方法,通常在這個run方法中寫入須要在UI現呈上進行的操做,關於post方式的方法:
a.boolean post(Runnable r):把一個Runnable對象入隊到消息隊列中,UI線程從消息隊列中取出這個對象後,當即執行
b.boolean postAtTime(Runnable r,long uptimeMillis):把一個Runnable對象入隊到消息隊列中,UI線程從消息隊列中取出這個對象後,在特定的時間執行
c.boolean postDelayed(Runnable r,long delayMillis):把一個Runnable對象入隊到消息隊列中,UI線程從消息隊列中取出這個對象後,延遲delayMillis秒執行
d.void removeCallbacks(Runnable r):從消息隊列中移除一個Runnable對象
e.代碼示例:
public class HandlerPostActivity extends Activity{
private Button btn1,btn2;
private TextView testView;
//聲明一個Handler對象
pirvate Handler handler=new Handler();
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1=(Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
testView=(TextView )findViewById(R.id.testView);
btn1.setOnClickListener(new View.OnClickListener(){
public void click(View v){
//新啓動一個子線程
new Thread(new Runnable(){
public void run(){
//使用post方式修改UI組件testView的text屬性
handler.post(new Runnable(){
public void run(){
testView.setText(「使用handler 的post方式在工做線程中發送一段執行到消息隊列中,在主線程中執行」);
}
}
}
}).start();
}
});
btn1.setOnClickListener(new View.OnClickListener(){
public void click(View v){
//新啓動一個子線程
new Thread(new Runnable(){
public void run(){
//使用postDelayed方式修改組件testView的text屬性
handler.postDelayed(new Runnable(){
public void run(){
testView.setText(「使用handler postDelayed發送一段到消息隊列中國,在主線程中延遲3s執行」);
}
},3000);
}
}).start();
}
});
}
注意:對於post方式而言,它其中Runnable對象的run方法,均執行在UI線程上,因此對於這段代碼而言,不能執行在UI線程上的操做,同樣沒法使用post方式
f.代碼示例2:使用post方式從互聯網上獲取一張圖片,並顯示ImageView中
public class HandlerPostActivity extends Activity{
private Button btn;
private ImageView imageView;
private static String image_path = "http://ww4.sinaimg.cn/bmiddle/786013a5jw1e7akotp4bcj20c80i3aao.jpg";
private ProgressDialog dialog;
//一個靜態的handler,handler建議聲明爲靜態的
private static Handler handler=new Handler();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask_activity);
btn=(Button)findViewById(R.id.btn);
imageView=(ImageView)findById(R.id.imageView);
//設置進度條提示
dialog=new ProgressDialog(this);
dialog.setTitle(「提示」);
dialog.setMessage(「正在下載,請稍後...」);
dialog.setCancelable(false);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//開啓一個子線程,用於下載圖片
new Thread(new MyThread()).start();
dialog.show();
}
});
}
public class MyThread implements Runnable{
public void run(){
//下載一個圖片
HttpClient client=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(image_path);
HttpReponse httpReponse=null;
//紅色字體需處理異常
httpResponse=client.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
byte[] data=EntityUtils.toByteArray(httpResponse.getEntity());
//獲得一個bitmap對象,而且爲了使可以在post訪問,必須聲明爲final
final Bitmap bmp=BitmapFactory.decodeByteArray(data,0,data.length);
handler.post(new Runnable(){
public void run(){
imageView.setImageBitmap(bmp);
}
});
//隱藏對話框
dialog.dismiss();
}
}
}
}
D.Message
a.Handler若是使用sendMessage的方式把消息入隊到消息隊列中,需傳遞一個Message對象,而在Handler中需重寫handleMessage()方法,用於獲取工做線程傳遞過來的消息,此方法應用在UI線程上
b.Message是一個final類,因此不可被繼承。Message封裝了線程中傳遞的消息,若是對於通常的數據,Message提供了getData()和setData()方法來獲取與設置參數,其中操做的數據是一個Bundle對象,
Bundle對象提供了一系列getXXX()和setXXX()方法用於傳遞基本數據類型的鍵值對。對於複雜的數據類型,Bundle提供了兩個方法:putParcelable(String key,Parcelable value):須要傳遞的對象類實現Parcelable接口;
pubSerializable(String key,Serializable value):須要傳遞的對象類實現Serializable接口。
c.另外一種方式在Message中傳遞對象,即便用Message自帶的obj屬性傳值,它是一個Object類型,能夠傳遞任意類型的對象,Message自帶以下屬性:
int arg1:參數一,用於傳遞不復雜的參數,複雜數據使用setData()傳遞
int arg2:參數二,用於傳遞不復雜的參數,複雜數據用setData()傳遞
Object obj:傳遞一個任意的對象
int what:定義的消息碼,通常用於設定消息的標誌
d.對於Message對象,通常不建議經過構造方法獲得,而是建議經過使用Message.obtain()靜態的方法或Handler.obtainMessage()獲取。Message.obtain()會從消息池中獲取一個Message對象,若是消息池是空的,纔會使用構造方法實例化一個Message對象,這樣有利於消息資源的利用。不用擔憂消息池中的消息過多,它的上限是10個
e.代碼示例:
public class HandleMessageActivity extends Activity{
private Button btn;
private ImageView imageView;
private static String image_path=」http://ww4.sinaimg.cn/bmiddle/786013a5jw1e7akotp4bcj20c80i3aao.jpg」;
private ProgressDialog dialog;
private static int IS_FINISH=1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask_activity);
btn=(Button)findViewById(R.id.btn);
imageView=(ImageView)findViewById(R.id.imageView);
//設置進度條提示
dialog=new ProgressDialog(this);
dialog.setTitle(「提示」);
dialog.setMessage(「正在下載,請稍後...」);
dialog.setCancelable(false);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//開啓一個子線程,用於下載圖片
new Thread(new MyThread()).start();
dialog.show();
}
});
}
private Handler handler=new Handler(){
//在Handler中獲取消息,重寫handleMessage()方法
public void handleMessage(Message msg){
//判斷消息碼是否爲空
if(msg.what==IS_FINISH){
byte[] data=(byte[])msg.obj;
Bitmap bmp=BitmapFactory.decodeByteArray(data,0,data.length);
imageView.setImageBitmap(bmp);
dialog.dismiss();
}
}
};
public class MyThread implements Runnable{
public void run(){
HttpClient client=new DefaultHttpClient();
HttpGet httpGet=new HttpGet(image_path);
HttpResponse httpResponse=null;
//紅色字體部分需處理異常
httpResponse=client.execute(httpGet);
if(httpResponse.getStatusLine().getStatusCode()==200){
byte[] data=EntityUtils.toByteArray(httpResponse.getEntity());
//獲取一個Message對象,設置what爲1
Message msg=Message.obtain();
msg.obj=data;
msg.what=IS_FINISH;
//發送這個消息到消息隊列中
handler.sendMessage(msg);
}
}
}
}
7.android使用http協議與服務器通訊(HttpURLConnection)
A.get方式:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask_activity);
//採用get方式訪問服務器
String strurl=」http://192.168.0.100:8080/AndroidJ2eeServlet/ServletTest?param=helloworld」;
URL url=null;
//紅色字體部分需處理異常
url=new URL(strurl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
InputStreamReader in=new InputStreamReader(conn.getInputStream());//向J2ee服務器發送消息
BufferedReader bufferReader=new BufferedReader(in);
String result=」」;
String readLine=null;
while((readLine=bufferReader.readLine())!=null){
result+=readLine;
}
in.close();
conn.disconnect();
TextView textView=(TextView)findViewById(R.id.textView);
textView.setText(result);
}
B.post方式:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask_activity);
//採用post方式向服務端傳送數據以及從服務器接收數據
String strUrl=」http://192.168.0.100:8080/AndroidJ2eeServlet/ServletTest」;
URL url=null;
//紅色字體部分需處理異常
url=new URL(strUrl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setDoInput(true);//設置輸入流採用字節流
conn.setOutput(true);//設置輸出流採用字節流
conn.setRequestMethod(「POST」);
conn.setUseCaches(false);//設置緩存
conn.setRequestProperty(「content-type」,」application/x-www-form-urlencoded」);//設置meta參數
conn.setRequestProperty(「Charset」,」utf-8」);
conn.connect();//鏈接既向服務器發送消息
DataOutputStream dop=new DataOutputStream(conn.getOutputStream());
dop.writeBytes(「param=」+URLEncode.encode(「Q:下週去釣魚」,」utf-8」));//發送參數
dop.flush();
dop.close();
//如下是接收工做
BufferedReader bufferReader=new BufferedReader(new InputStreamReader(conn.getInputStream()));
String result=」」;
String readLine=null;
while((readLine=bufferReader.readLine())!=null){
result+=readLine;
}
bufferReader.close();
conn.disconnect();
//將接收到的數據顯示出來
TextView textView=(TextView)findViewById(R.id.result);
textView.setText(URLDecode.decode(result,」utf-8」);
}
8.android界面
A.android生成屏幕有三種方式:xml配置生成;經過用戶界面接口生成;直接用代碼生成
B.View類,任何一個View對象都繼承android.View類,它是一個存儲有屏幕上特定的一個矩形佈局和內容屬性的一個佈局。一個View對象能夠處理測距、佈局、焦點變換和滾動條,以及屏幕區域本身表現的按鍵和手勢。做爲一個基類,一個view對象爲Widget,Widget則是一組用於繪製交互屏幕元素的徹底實現子類,Widget能夠處理本身的測距和繪圖,因此能夠用它們快速的構建UI,可用到的Widget包括Text,EditText,Button,RadioButton,CheckBox和ScrollView
C.ViewGroup,是一個android.view,ViewGroup的對象,ViewGroup是一個特殊的View對象,它的功能是裝載和管理一組下層的View和其餘ViewGroup
D.TextView對象,示例:
TextView textView=(TextView)findById(R.id.textView);
textView.setTextColor(Color.RED);
textView.setTextSize(20);
textView.setBckgroundColor(Color.BLUE);
textView.setString(「我是一個文本」);
E.Toast,提示類:
Toast toast=Toast.makeText(this,」我是一個提示」,Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP,0,220);//設置提示的位置
toast.show();//顯示提示
F.單項選擇:RadioButton,RadioGroup,選項按鈕能夠經過RadioButton來實現,「答案」則經過RadioGroup 來實現,在xml配置文件中:
<RadioButton...>
<RadioGroup.../>
<RadioGroup.../>
</RadioButton>
G多項選擇(CheckBox),xml配置文件
<CheckBox../>
<CheckBox.../>
<CheckBox.../>
H.下拉列表:Spinner,xml文件
<Spinner android;id=」@+id/spinner」
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:layout_cneterHorizontal=」true」/>
java代碼:
priavate static final String[] items={「O型」,」A型」,」B型」,」AB型」};
Spinner spinner=(Spinner)findById(R.id.spinner);
//將可選內容與ArrayAdapter關聯
ArrayAdapter adapter=new ArrayAdapter<String>(this,R.layout.simple_spinner_item,items);
//設置下拉列表的風格
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
//將adapter添加到spinner中
spinner.setAdapter(adapter);
I.自動提示(AutoCompeleteTextView)
J.日期和時間(DatePicker,TimePicker):首先須要在佈局文件中定義DatePicker和TimePicker,而後經過Calendar獲取系統日期,接着經過init方法將日期傳給DatePicker,並設置 OnDateChangedListener來監聽日期改變,當時間被改變設置setOnTimeChangedListener監聽時間的改變,
java文件:
public void onCreate(Bundle savedInstanceState){
super.onCreated(saveInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
//獲取DatePicker對象
DatePicker datePicker=(DatePicker)findViewById(R.id.datePicker);
//將日曆初始化爲當前時間,並設置監聽事件
datePicker.init(c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH),new DatePicker.OnDateChangedListener(){
@Override
public void onDateChanged(DatePicker view,int year,int monthOfYear,int dayOfMonth){
//當日期更改時在這裏進行處理
c.set(year,monthOfYear,dayOfMonth);
}
});
//獲取TimePicker對象
TimePicker timePicker=(TimePicker)findViewById(R.id.timePicker);
//設置爲24小時顯示制
timePicker.setIs24HourView(true);
timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener(){
@Override
public void onTimeChanged(TimePicker view,int hourOfDay,int minute){
//時間改變處理
c.set(year,month,day,hourOfDay,minute,second);
}
});
}
xml配置文件:
<DatePicker
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」
android:id=」@+id/datePicker」>
</DatePicker>
<TimePicker
android:id=」@+id/timePicker」
android:layout_width=」wrap_content」
android:layout_height=」wrap_content」>
</TimePicker>
9.android小知識:
A.實例化一個res/layout/main.xml:經過類LayoutInflate能夠對佈局xml文件實例化
a.LayoutInflate inflate=LayoutInflate.from(Context context);進而能夠建立一個試圖
View view=inflate.inflate(R.layout.main,null);//即建立了main.xml文件的試圖,能夠動態的建立或者修改改xml文件
b.LayoutInflater inflater=getLayoutInflater();//調用activity的getLayoutInflater();方法
c.LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
B.建立Activity時,若是單擊事件較多,Activity可實現OnClickListener接口;
View continueBtn=findViewById(R.id.continue_btn);
continueBtn.setOnClickListener(this);
View newBtn=findViewById(R.id.new_btn);
newBtn.setOnClickListener(this);
//實現的單擊方法
public void click(View v){
switch(v.getId()){
case R.id.continue_btn:
//執行一些操做
break;
case R.id.new_btn:
//執行一些操做
break;
default:
break;
}
}
10.菜單(menu):
兩種方式建立菜單:
經過xml佈局文件建立:
<menu xmlnx=」」>
<item android:id=」@+id/about」
android:title=」關於」/>
<item android:id=」@+id/exit」
android:title=」退出」/>
</menu>
java代碼:
//建立發方法
public boolean onCreateOptionsMenu(Menu menu){
MenuInflate inflater=getMenuInflater();
//設置menu界面爲menu.xml
inflater.inflater(R.menu.menu,menu);
return true;
}
經過menu.add方法添加
//建立發方法
public void onCreateOptionsMenu(Menu menu){
menu.add(0,0,0,R.string.ok);
menu.add(0,1,1,R.string.back);
return true;
}
11.A.ImageView:將一張圖片顯示在屏幕上,須要建立一個顯示圖片的對象,這個對象就是ImageView,而後經過setImageResource來設置要顯示的圖片資源索引
B.ImageButton:要建立帶圖標的Button,首先要在佈局文件中定義ImageButton,而後經過setImageDrawable方法來設置按鈕要顯示的圖標
C.拖動效果(Gallery)
D.切換圖片(ImageSwitcher)
E.網格視圖(GridView):GridView的視圖排列方式與矩陣相似,當屏幕上由不少元素(文字,圖片或其餘元素)須要顯示時可使用GridView來顯示。既然有多個元素須要顯示,就要使用BaseAdapter來存儲這些元素
F.ScrollView卷軸視圖:
G.進度條ProgressBar,android提供兩類進度條樣式:長型進度條和圓型進度條
H.拖動條SeekBar:拖動條相似進度條,不一樣的是拖動條用戶能夠控制;因爲拖動條用戶能夠控制,因此須要對其進行事件監聽,須要實現SeekBar.OnSeekBarChangeListener接口,在 SeekBar中須要監聽三個事件:數值的改變(onProgressChanged)、開始拖動(onStartTackingTouch)、中止拖動(onStopTrackingTouch)
I.狀態欄提示(Notification,NotificationManager):android提供了NotificationManager來管理狀態欄信息,提供Notification來處理這些訊息。首先經過getSystenService()獲得 NotificationManager對象,咱們能夠對Notification的內容、標題和圖標等屬性進行設置。,而後經過notify方法來執行一個Notification快訊
J.TabWidget:TabWidget相似電話薄中的界面,經過多個標籤切換顯示不一樣的內容。首先需瞭解TabHost,它是一個用來存放多個Tab標籤的容器
12.Bitmap類:用於獲取圖像文件信息,進行圖像的旋轉、平移和縮放等,並能夠指定保存文件格式
A.能夠經過來獲取該圖像對象Bitmap:
Bitmap bitmap=(BitmapDrawable)getResource().getDrawable(R.drawable.fuwa).getBitmap();
B.得到圖像資源後,可使用drawBitmap()方法將圖像顯示到屏幕(x,y)座標上,方法:Canvas.drawBitmap(bitmap,x,y,null);
C.要得到圖像信息,能夠經過bitmap.getHeight()得到圖像高度,經過bitmap.getWidth()得到圖像寬度
D.實現圖像的平移,只需改變Canvas.drawBitmap(bitmap,x,y,null)方法中的x,y,便可
E.圖像的旋轉:用Matrix實現圖像旋轉,Matrix是一個3*3的矩陣,專門用於圖像變換匹配。Matrix沒有結構體,必須初始化,能夠經過reset()或set()方法實現嗎matrix.reset()
初始化以後,就可使用setRotate()方法設置旋轉的角度:matrix.setRotate()。
旋轉角度設置完畢以後,可使用createBitmap()方法建立一個通過旋轉處理的Bitmap對象
bitmapRotate=Bitmap.createBitmap(bitmap,0,0,bitmapWidth,bitmapHeight,matrix,true);
13.android顏色編碼表:
<resources>
<color name="white">#ffffff</color><!--白色 --> <color name="ivory">#fffff0</color><!--象牙色 -->
<color name="lightyellow">#ffffe0</color><!--亮黃色 --> <color name="yellow">#ffff00</color><!--黃色 -->
<color name="snow">#fffafa</color><!--雪白色 --> <color name="floralwhite">#fffaf0</color><!--花白色 -->
<color name="lemonchiffon">#fffacd</color><!--檸檬綢色 --> <color name="cornsilk">#fff8dc</color><!--米綢色 -->
<color name="seaShell">#fff5ee</color><!--海貝色 --> <color name="lavenderblush">#fff0f5</color><!--淡紫紅 -->
<color name="papayawhip">#ffefd5</color><!--番木色 --> <color name="blanchedalmond">#ffebcd</color><!--白杏色 -->
<color name="mistyrose">#ffe4e1</color><!--淺玫瑰色 --> <color name="bisque">#ffe4c4</color><!--桔黃色 -->
<color name="moccasin">#ffe4b5</color><!--鹿皮色 --> <color name="navajowhite">#ffdead</color><!--納瓦白 -->
<color name="peachpuff">#ffdab9</color><!--桃色 --> <color name="gold">#ffd700</color><!--金色 -->
<color name="pink">#ffc0cb</color><!--粉紅色 --> <color name="lightpink">#ffb6c1</color><!--亮粉紅色 -->
<color name="orange">#ffa500</color><!--橙色 --> <color name="lightsalmon">#ffa07a</color><!--亮肉色 -->
<color name="darkorange">#ff8c00</color><!--暗桔黃色 --> <color name="coral">#ff7f50</color><!--珊瑚色 -->
<color name="hotpink">#ff69b4</color><!--熱粉紅色 --> <color name="tomato">#ff6347</color><!--西紅柿色 -->
<color name="orangered">#ff4500</color><!--紅橙色 --> <color name="deeppink">#ff1493</color><!--深粉紅色 -->
<color name="fuchsia">#ff00ff</color><!--紫紅色 --> <color name="magenta">#ff00ff</color><!--紅紫色 -->
<color name="red">#ff0000</color><!--紅色 --> <color name="oldlace">#fdf5e6</color><!--老花色 -->
<color name="lightgoldenrodyellow">#fafad2</color><!--亮金黃色 --> <color name="linen">#faf0e6</color><!--亞麻色 -->
<color name="antiquewhite">#faebd7</color><!--古董白 --> <color name="salmon">#fa8072</color><!--鮮肉色 -->
<color name="ghostwhite">#f8f8ff</color><!--幽靈白 --> <color name="mintcream">#f5fffa</color><!--薄荷色 -->
<color name="whitesmoke">#f5f5f5</color><!--煙白色 --> <color name="beige">#f5f5dc</color><!--米色 -->
<color name="wheat">#f5deb3</color><!--淺黃色 --> <color name="sandybrown">#f4a460</color><!--沙褐色 -->
<color name="azure">#f0ffff</color><!--天藍色 --> <color name="honeydew">#f0fff0</color><!--蜜色 -->
<color name="aliceblue">#f0f8ff</color><!--艾利斯蘭 --> <color name="khaki">#f0e68c</color><!--黃褐色 -->
<color name="lightcoral">#f08080</color><!--亮珊瑚色 --> <color name="palegoldenrod">#eee8aa</color><!--蒼麒麟色 -->
<color name="violet">#ee82ee</color><!--紫羅蘭色 --> <color name="darksalmon">#e9967a</color><!--暗肉色 -->
<color name="lavender">#e6e6fa</color><!--淡紫色 --> <color name="lightcyan">#e0ffff</color><!--亮青色 -->
<color name="burlywood">#deb887</color><!--實木色 --> <color name="plum">#dda0dd</color><!--洋李色 -->
<color name="gainsboro">#dcdcdc</color><!--淡灰色 --> <color name="crimson">#dc143c</color><!--暗深紅色 -->
<color name="palevioletred">#db7093</color><!--蒼紫羅蘭色 --> <color name="goldenrod">#daa520</color><!--金麒麟色 -->
<color name="orchid">#da70d6</color><!--淡紫色 --> <color name="thistle">#d8bfd8</color><!--薊色 -->
<color name="lightgray">#d3d3d3</color><!--亮灰色 --> <color name="lightgrey">#d3d3d3</color><!--亮灰色 -->
<color name="tan">#d2b48c</color><!--茶色 --> <color name="chocolate">#d2691e</color><!--巧可力色 -->
<color name="peru">#cd853f</color><!--祕魯色 --> <color name="indianred">#cd5c5c</color><!--印第安紅 -->
<color name="mediumvioletred">#c71585</color><!--中紫羅蘭色 --> <color name="silver">#c0c0c0</color><!--銀色 -->
<color name="darkkhaki">#bdb76b</color><!--暗黃褐色 --> <color name="rosybrown">#bc8f8f</color><!--褐玫瑰紅 -->
<color name="mediumorchid">#ba55d3</color><!--中粉紫色 --> <color name="darkgoldenrod">#b8860b</color><!--暗金黃色 -->
<color name="firebrick">#b22222</color><!--火磚色 --> <color name="powderblue">#b0e0e6</color><!--粉藍色 -->
<color name="lightsteelblue">#b0c4de</color><!--亮鋼蘭色 --> <color name="paleturquoise">#afeeee</color><!--蒼寶石綠 -->
<color name="greenyellow">#adff2f</color><!--黃綠色 --> <color name="lightblue">#add8e6</color><!--亮藍色 -->
<color name="darkgray">#a9a9a9</color><!--暗灰色 --> <color name="darkgrey">#a9a9a9</color><!--暗灰色 -->
<color name="brown">#a52a2a</color><!--褐色 --> <color name="sienna">#a0522d</color><!--赭色 -->
<color name="darkorchid">#9932cc</color><!--暗紫色 --> <color name="palegreen">#98fb98</color><!--蒼綠色 -->
<color name="darkviolet">#9400d3</color><!--暗紫羅蘭色 --> <color name="mediumpurple">#9370db</color><!--中紫色 -->
<color name="lightgreen">#90ee90</color><!--亮綠色 --> <color name="darkseagreen">#8fbc8f</color><!--暗海蘭色 -->
<color name="saddlebrown">#8b4513</color><!--重褐色 --> <color name="darkmagenta">#8b008b</color><!--暗洋紅 -->
<color name="darkred">#8b0000</color><!--暗紅色 --> <color name="blueviolet">#8a2be2</color><!--紫羅蘭藍色 -->
<color name="lightskyblue">#87cefa</color><!--亮天藍色 --> <color name="skyblue">#87ceeb</color><!--天藍色 -->
<color name="gray">#808080</color><!--灰色 --> <color name="grey">#808080</color><!--灰色 -->
<color name="olive">#808000</color><!--橄欖色 --> <color name="purple">#800080</color><!--紫色 -->
<color name="maroon">#800000</color><!--粟色 --> <color name="aquamarine">#7fffd4</color><!--碧綠色 -->
<color name="chartreuse">#7fff00</color><!--黃綠色 --> <color name="lawngreen">#7cfc00</color><!--草綠色 -->
<color name="mediumslateblue">#7b68ee</color><!--中暗藍色 --> <color name="lightslategray">#778899</color><!--亮藍灰 -->
<color name="lightslategrey">#778899</color><!--亮藍灰 --> <color name="slategray">#708090</color><!--灰石色 -->
<color name="slategrey">#708090</color><!--灰石色 --> <color name="olivedrab">#6b8e23</color><!--深綠褐色 -->
<color name="slateblue">#6a5acd</color><!--石藍色 --> <color name="dimgray">#696969</color><!--暗灰色 -->
<color name="dimgrey">#696969</color><!--暗灰色 --> <color name="mediumaquamarine">#66cdaa</color><!--中綠色 -->
<color name="cornflowerblue">#6495ed</color><!--菊蘭色 --> <color name="cadetblue">#5f9ea0</color><!--軍蘭色 -->
<color name="darkolivegreen">#556b2f</color><!--暗橄欖綠 --> <color name="indigo">#4b0082</color><!--靛青色 -->
<color name="mediumturquoise">#48d1cc</color><!--中綠寶石 --> <color name="darkslateblue">#483d8b</color><!--暗灰藍色 -->
<color name="steelblue">#4682b4</color><!--鋼蘭色 --> <color name="royalblue">#4169e1</color><!--皇家藍 -->
<color name="turquoise">#40e0d0</color><!--青綠色 --> <color name="mediumseagreen">#3cb371</color><!--中海藍 -->
<color name="limegreen">#32cd32</color><!--橙綠色 --> <color name="darkslategray">#2f4f4f</color><!--暗瓦灰色 -->
<color name="darkslategrey">#2f4f4f</color><!--暗瓦灰色 --> <color name="seagreen">#2e8b57</color><!--海綠色 -->
<color name="forestgreen">#228b22</color><!--森林綠 --> <color name="lightseagreen">#20b2aa</color><!--亮海藍色 -->
<color name="dodgerblue">#1e90ff</color><!--閃蘭色 --> <color name="midnightblue">#191970</color><!--中灰蘭色 -->
<color name="aqua">#00ffff</color><!--淺綠色 --> <color name="cyan">#00ffff</color><!--青色 -->
<color name="springgreen">#00ff7f</color><!--春綠色 --> <color name="lime">#00ff00</color><!--酸橙色 -->
<color name="mediumspringgreen">#00fa9a</color><!--中春綠色 --> <color name="darkturquoise">#00ced1</color><!--暗寶石綠 -->
<color name="deepskyblue">#00bfff</color><!--深天藍色 --> <color name="darkcyan">#008b8b</color><!--暗青色 -->
<color name="teal">#008080</color><!--水鴨色 --> <color name="green">#008000</color><!--綠色 -->
<color name="darkgreen">#006400</color><!--暗綠色 --> <color name="blue">#0000ff</color><!--藍色 -->
<color name="mediumblue">#0000cd</color><!--中蘭色 --> <color name="darkblue">#00008b</color><!--暗藍色 -->
<color name="navy">#000080</color><!--海軍色 --> <color name="black">#000000</color><!--黑色 -->
</resources>
14.使用HttpClient接口實現網絡通訊:
A.DefaultHttpClient:DefaultHttpClient是HttpClient的一個直接子類,是一個默認的HTTP客戶端,可使用它建立一個HTTP鏈接。在HTTP中提供了多個重載的execute()方法,用來執行http請求
B.HttpRequest接口:HttpGet和HttpPost是HttpRequest的直接子類,分別用於向服務器提交get請求和post請求
C.HttpResponse接口:HttpResponse接口提供了Http的響應信息,其中getEntity() 方法用於得到響應消息的實體,getStatusLine()方法用於獲取響應的狀態行,在返回結果statusLine中包含了HTTP響應的狀態碼,可使用方法getStatusCode()方法得到
15.ViewGroup.MarginLayoutParams用於控制子組件周圍的頁邊距:
android:layout_marginBottom該子組件下邊的頁邊距;android:layout_marginLeft該子組件左邊的頁邊距;
android:layout_marginRight該子組件右邊的頁邊距;android:layout_marginTop: 該子組件上邊的頁邊距
對於view的尺寸,android給了三種單位供選擇使用:
px:像素;dp:dip,表示屏幕實際的像素;
sp:會根據系統設定的字體大小進行縮放,與dp相似,一般用與設置字體
尺寸單位的選擇技巧:若是設置長度、高度等屬性可使用dp;若是設置字體,推薦使用sp;在屏幕適配方面,不推薦使用px,由於使用dp或sp,UI會根據設備的density和scaleDensity進行等比例縮放,以達到不一樣屏幕適配的效果,讓其在不一樣的屏幕看起來差很少的效果。
16.子activity向父activity返回結果,實現子activity向父activity發送返回信息,有如下兩種方法可調用:
public final void setResult(int resultCode);
public final void setResult(int resultCode,Intent data);
一般來講,resultCode是如下兩個預約義常量的任意一個:
Activity.RESULT_OK;Activity.RESULT_CANCELED;若是需自定義結果代碼,還可使用另外一個常量:RESULT_FIRST_USER;
17.fragment
A. fragment的特性:
a. Fragment老是做爲Activity界面組成部分,Fragment能夠經過getActivity()方法獲取所在的activity,activity能夠經過調用FragmentManager的findFragmentById()或findFragmentByTag()方法獲取fragment;
b. 在activity運行時,可調用FragmentManager的add(),remove(),replace()方法動態的操做Fragment
c. 一個Activity可同時組合多個Fragment,一個Fragment能夠被多個Activity複用
d. Fragment能夠響應輸入事件並有本身的生命週期,但其生命週期被其所在的activity生命週期所控制
B. 建立Fragment:
a. 建立Fragment須要繼承Fragment基類或Fragment的子類,Fragment的子類有:DialogFragment,ListFragment,PreferenceFragment或者WebViewFragment,與建立activity相似,fragment須要實現一些回調方法:onCreate(),onCreateView(),onStart(),onResume(),onPause(),onStop()等,一般會重寫如下三個方法:
onCreate():系統建立Fragment後回調該方法,初始化必要組件
onCreateView():當Fragment繪製界面組件時調用該方法,該方法返回一個view,該view就是Fragment顯示的view
onPause():離開fragment調用該方法
b. 代碼示例:
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
//加載fragment_main.xml佈局文件
View rootView=inflater.inflate(R.layout.fragment_main,container,false);
return rootView;
}
C. Fragment添加到Activity:建立好的Fragment只有添加到Activity才能起做用,添加到Activity有兩種方法:
a. 在佈局文件中使用<Fragment…/>元素添加Fragment,android:name指定Fragment的實現類;
<LinearLayout …>
<Fragment android:name=」com.myfragment.test.MyFragment」
android:id=」@+id/fragment」
android:layout_width=」wrap_content」
android:layout_height=」match_parent」/>
</LinearLayout>
注:每一個fragment都須要提供一個惟一的標識,爲fragment提供id有三種方法:
① android:id屬性提供一個惟一的標識
② 用android:tag屬性提供一個惟一的字符串
③ 若是上述兩個屬性都沒有,系統會使用其容器視圖(View)的id
b. 在java代碼中,經過FragmentTransaction對象的add()方法來添加Fragment
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//加載容器
setContentView(R.layout.activity_main);
//添加fragment
getFragmentManager().beginTransaction().add(R.id.container,new MyFragment()).commit();
}
activity_main.xml佈局文件以下所示:
<FrameLayout xmlns:android=」…」
android:id=」@+id/container」
android:layout_width=」match_parent」
android:layout_height=」match_parent」/>
注:Activity的getFragmentManager()方法能夠返回FragmentManager,FragmentManager的beginTransaction()方法可開啓並返回FragmentTransaction對象
D. Fragment與Activity通訊
a. Fragment添加到Activity後,fragment和activity必須實現通訊,這就須要fragment可以獲取activity,activity可以獲取到fragment:
Fragment獲取它所在的Activity:調用fragment的getActivity()便可獲取到fragment的activity
Activity獲取它所擁有的Fragment:調用Activity關聯的FragmentManager的findFragmentById(int id)或findFragmentByTag(String tag)便可獲取指定的fragment
注:在佈局文件中<Fragment…/>元素添加fragment時,能夠指定android:id或android:tag屬性,用於標識該fragment
b. Fragment與Activity傳遞數據:
Activity向Fragment傳遞數據:在Activity中建立Bundle數據包,並調用fragment的setArguments(Bundle bundle)方法將Bundle數據包傳遞給fragment
Fragment向Activity傳遞數據或activity須要在Fragment運行時實時獲取數據:在Fragment中定義一個內部回調接口,再讓包含該Fragment的Activity實現回調接口
E. Fragment管理與Fragment事務:
a.Activity管理Fragment主要經過FragmentManager,經過FragmentManager能夠完成如下幾個功能:
使用findFragmentById()和findFragmentByTag()獲取指定的Fragment
調用popBackStack()將Fragment從後臺棧中彈出,
調用addOnBackStackChangeListener()註冊一個監聽器,用於監聽後臺棧的變化
c. 藉助FragmentTransaction實現刪除、添加、替換Fragment,FragmentTransaction表明Activity對Fragment執行的多個改變,代碼示例:
//打開事務,獲取FragmentManager
FragmentManager fm=getFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
//建立一個新的Fragment
Fragment fragment=new MyFragment();
//替換container容器的fragment
transaction.replace(R.id.container,fragment);
//將事務添加到back棧,容許用戶按下back鍵返回到上一個狀態
transaction.addToBackStack(null);
transaction.commit();
18.weight屬性詳解:
A. layout_gravity和gravity區別:layout_gravity表示的是組件自身在父組件中的位置,gravity表示的是組件的子組件在組件中的位置
B. 做爲父layout的LinearLayout的屬性android:orientation=」vartical」時,android:layout_gravity屬性設爲橫向的時候才能生效,如left,right,
center_horizontal;當android:orientation=」horizontal」時,android:layout_gravity屬性設爲縱向的時候才能生效,如top,bottom,
center_vertical