shihyingru
4/7/2017 - 3:35 AM

getScreenSize() && onTouchEvent()

getScreenSize() //判斷螢幕直式或橫式
//weightSum_layoutWeight //onTouchEvent與onTouch

依比例布局layout_weight和weightSum

範例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:background="#ffffff"  
    android:gravity="center"  
    android:orientation="horizontal"  
    android:weightSum="1" >  //加上weightSum就是要內部原件依剩餘空間分配
  
    <Button  
        android:layout_width="0dp"  //要設置為0才能依權重剩餘空間分配
        android:layout_height="wrap_content"  
        android:layout_weight="0.5"  //佔寬度的一半0.5
        android:text="@string/activity_main_click_me" />  
  
</LinearLayout> 

計算指令(依以上程式碼為例)

Button's width + Button's weight * 200 / weightSum = 0 + 0.5 * 200 / 1 = 100

public void onConfigurationChanged(Configuration configuration) {

        super.onConfigurationChanged(configuration);

        //橫轉直會多跑一次 , 所以加上判斷過濾
        if (oldOrientation != configuration.orientation) {
            if (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {

                View dectorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
                dectorView.setSystemUiVisibility(uiOptions);

                setContentView(R.layout.activity_search_page);
            } else
                setContentView(R.layout.activity_search_page);
            oldOrientation = configuration.orientation;
        }

    }

(https://dotblogs.com.tw/nai0920/2015/10/16/153588)

一、禁止螢幕旋轉:

打開工程中的 AndroidManifest.xml 檔,在 中,添加一條屬性資訊:

android:screenOrientation="portrait" (強制豎屏)

android:screenOrientation="landscape" (強制橫屏)

二、螢幕旋轉不刷新 Activity:

當螢幕旋轉時,會重新執行 Activity 中的 onCreate() 方法,即刷新了 Activity 的顯示。若是不想刷新,可以用第一種方法鎖屏,也可以用以下方法:

1、打開工程中的 AndroidManifest.xml 檔,在 中,添加一條屬性資訊:

android:configChanges=”orientation|keyboardHidden”

注:這個屬性指的是,當後邊屬性值代表的事件發生時,Activity 會執行某個函數,orientation 指的是當螢幕旋轉時,keyboardHidden 指的是鍵盤協助工具改變。“|”為或符號,指這兩個中任意一個發生,就執行 Activity 某個函數。

注2:如果你的開發 API 等級等於或高於 13,你還需要設置 screenSize,因為 screenSize 會在螢幕旋轉時改變。

2、在對應 Activity 中重寫 onConfigurationChanged() 方法:

@Override  
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 什麼都不用寫
    }
    else {
        // 什麼都不用寫
    }
}

注:如果在 if、else 中,使用了 setContentView(R.layout.xxxx) 函數,那麼就可以實現:每次螢幕旋轉時,調用不同的佈局。

手勢偵測onTouchEvent與onTouch牴觸事件

(http://www.jianshu.com/p/68a31d8ad5f0)

1.onTouch和onTouchEvent都是在dispatchTouchEvent方法中被调用的方法。onTouch会优先于onTouchEvent被执行。

2.如果onTouch通过返回true将事件消费掉,事件便不会传递到onTouchEvent中。特别要强调的一点是,只有当mOnTouchListener不为null并且控件是enabled,onTouch方法才会得到执行。

3.dispatchTouchEvent在进行事件分发时,如果某个ACTION返回了false,那么后面的ACTION都将得不到执行。

4.setOnClickListener方法会设置view为可点击。

//    int ScreenWidth, ScreenHeight, oldOrientation = -1;
//Configuration.ORIENTATION_LANDSCAPE Constant Value: 2
//Configuration.ORIENTATION_PORTRAIT Constant Value: 1
//get screen data(display's width&height)
        dm = getResources().getDisplayMetrics();
        ScreenWidth = dm.widthPixels;
        ScreenHeight = dm.heightPixels;

        if (ScreenHeight > ScreenWidth) {
            setContentView(R.layout.activity_search_page);
        }
        else {
            //hide the status bar
            View dectorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
            dectorView.setSystemUiVisibility(uiOptions);

            initviewPager();

            setContentView(R.layout.activity_search_page);
        }//get screen data finish.