2016年6月27日 星期一

Android中padding與layout_margin的區別與用法

   林炳文Evankaka原創作品。轉載請注明出處http://blog.csdn.net/evankaka

 一、定義   

        android:layout_margin就是設置view的上下左右邊框的額外空間
        android:padding是設置內容相對view的邊框的距離
        padding,含義為“填充”,像墊肩壓類似的填充物,一個控制項的padding及此控制項內部的填充,由此可見padding是以所被定義的控制項Aparent控制項,而內部的內容物與控制項A的間距。而layout_marginA控制項所在的控制項為parent控制項,是A與其的間距。
        其實概念很簡單,padding是站在父view的角度描述問題,它規定它裡面的內容必須與這個父view邊界的距離。margin則是站在自己的角度描述問題,規定自己和其他(上下左右)的view之間的距離,如果同一級只有一個view,那麼它的效果基本上就和padding一樣了
http://img.blog.csdn.net/20150303131301841?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRXZhbmtha2E=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
1.  當按鈕分別設置以上兩個屬性時,得到的效果是不一樣的。  
2.  android:paddingLeft="30px"  
3.  按鈕上設置的內容(例如圖片)離按鈕左邊邊界30個圖元  
4.  android:layout_marginLeft="30px"  
5.  整個按鈕離左邊設置的內容30個圖元  



二、使用示例

1、兩個都不加
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
1.  xml version="1.0" encoding="utf-8"?>  
2.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.      android:layout_width="match_parent"  
4.      android:layout_height="match_parent"  
5.      android:background="#ffffff" >  
6.    
7.      <TextView  
8.          android:layout_width="100dip"  
9.          android:layout_height="100dip"  
10.          android:background="#000000"  
11.         android:text="Hello" />  
12.   
13. </RelativeLayout>  
效果:
http://img.blog.csdn.net/20150303132257741?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRXZhbmtha2E=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

2、只加padding
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
1.  xml version="1.0" encoding="utf-8"?>  
2.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.      android:layout_width="match_parent"  
4.      android:layout_height="match_parent"  
5.       android:padding ="100dip"  
6.      android:background="#ffffff" >  
7.    
8.      <TextView  
9.          android:layout_width="100dip"  
10.         android:layout_height="100dip"  
11.          android:background="#000000"  
12.         android:text="Hello" />  
13.   
14. </RelativeLayout>  
效果:
http://img.blog.csdn.net/20150303132407088?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRXZhbmtha2E=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
3、只加margin
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
1.  xml version="1.0" encoding="utf-8"?>  
2.  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
3.      android:layout_width="match_parent"  
4.      android:layout_height="match_parent"  
5.      android:background="#ffffff" >  
6.    
7.      <TextView  
8.          android:layout_width="100dip"  
9.          android:layout_height="100dip"  
10.          android:background="#000000"  
11.          android:layout_margin="30dip"  
12.         android:text="Hello" />  
13.   
14. </RelativeLayout>  
效果:
http://img.blog.csdn.net/20150303132606979?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRXZhbmtha2E=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

三、注意說明

        LinearLayoutRelativeLayoutTableLayout中,這2個屬性都是設置都是有效的
        FrameLayout中,android:layout_margin是無效的,因為FrameLayout裡面的元素都是從左上角開始繪製的
        AbsoluteLayout中,沒有android:layout_margin屬性
        在此提醒,xml參數中含有layout的參數專案為定義的控制項相對于parent的關聯,沒有的一般為本身的定義,以上內容與此相符。又類似於gravitylayout_gravity,layout的是相對於parent的大體位置,而不帶的是自身內部內容的大體位置。

   林炳文Evankaka原創作品。轉載請注明出處http://blog.csdn.net/evankaka

[轉][Android]禁止旋轉螢幕時重新載入畫面(Don't reload, when rotate device)

Activity在執行時,可以直向或橫向顯示,當旋轉螢幕時,系統會自動做翻轉的動作。這在沒有載入資料的Activity中是沒問題的,但若有些資料是從網路下載下來,當旋轉螢幕時,會全部重新執行一次,因為Android對旋轉的做法是,把原先的Activity給Destroy掉,然後再Create一個新的不同方向的Activity。


如果不想讓Activity在旋轉螢幕時被自動轉向,可以在AndroidManifest.xml中設定Activity的屬性:
  • android:screenOrientation="portrait" (指定為直向)
  • android:configChanges="keyboard|keyboardHidden|orientation" (告訴系統,我要自己處理轉向問題)
android:configChanges="keyboard|keyboardHidden|orientation" 
android:screenOrientation="portrait">
原本我以為只要指定configChanges="orientation"來處理轉向就好,但不能單只有這個,還必須加上keyboard及keyboardHidden這兩個當螢幕鍵盤出現的時候也要處理,才能正常執行。

如此你可以在Activity的onConfigurationChanged方法中自己處理螢幕旋時要如何處理。

以下會做兩個測試,一個是有鎖定螢幕方向,一個沒有。

測試的程式碼:
public class RotationDeviceDemoActivity extends Activity {

    private final String TAG = "==DEMO==";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Log.d(TAG, "onCreate");
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        Log.d(TAG, "onConfigurationChanged");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy");
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        Log.d(TAG, "onRestoreInstanceState");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState");
    }
}

測試1:當Activity沒有設定configChanges及screenOrientation時(無鎖定螢幕方向)

執行結果:

523: D/==DEMO==(227): onCreate <===第一次開啟Activity
823: D/==DEMO==(227): onSaveInstanceState <===旋轉螢幕時可讓你儲存狀態
823: D/==DEMO==(227): onDestroy <===接著銷毀Activity
883: D/==DEMO==(227): onCreate <===然後從新建立一個新的已轉向的Activity
883: D/==DEMO==(227): onRestoreInstanceState <===並且可讓你取回剛才存下來的狀態

測試2:有設定configChanges及screenOrientation(鎖定螢幕方向)

執行結果:

184: D/==DEMO==(324): onCreate <===第一次開啟Activity
074: D/==DEMO==(324): onConfigurationChanged <===旋轉螢幕
554: D/==DEMO==(324): onConfigurationChanged <===再轉一次

你會發現,Activity這時不會儲存狀態及銷毀,它只會執行onConfigurationChanged,不管你轉幾次方向,都只會觸發這方法。你可以自行在這裡指定不同的layout佈局,或什麼都不做。
本文網址:http://blog.tonycube.com/2011/10/dont-reload-when-rotate-device.html

2015年12月4日 星期五

[Android][Adb]How to Capture Screenshot and Record Screen using ADB

1. Install the required ADB drivers on your PC.
2. Connect your device with PC using wired or wireless ADB connection.
3. Open Terminal in Linux or Command Prompt and enter the following command.
% adb shell screencap -p /sdcard/screencap.png
4. Now the captured screenshot will be placed on your internal sdcard. To copy the screenshot to your pc enter the command.
% adb pull /sdcard/screencap.png
Recording Screen
1. Connect your device with PC using wired or wireless ADB connection.
2. Open Terminal in Linux or Command Prompt and enter the following command.
% adb shell screenrecord /sdcard/screen.mp4
3. To stop screen recording break using Ctrl + C
4. Now the captured video will be placed in internal sdcard. Copy the video to PC using the command.
% adb pull /sdcard/screen.mp4
5. Increase bitrate of the video using the command.
adb shell screenrecord –bit-rate 12000000 /sdcard/screen.mp4