【Android學習】關於Android4.4沉浸模式的進入和退出時系統欄狀態欄遮蓋問題

在學習Android4.4沉浸模式的時候,點擊打開鏈接的博主將下面的問題寫的比較全面,大家可以參考一下。

  •     沉浸模式進入退出時系統欄和狀態欄會遮蓋畫面問題
  •     怎麼樣進入Activity和切換Activity時切換沉浸模式

不過補充一點:在退出沉浸模式時將系統欄和狀態欄恢復的時候,調用了setSystemUiVisibility後直接去取畫面的偏移高度Top的時候

<pre name="code" class="java">Rect frame = new Rect();
topView.getWindowVisibleDisplayFrame(frame) ;
int height = frame.top;


heightの值爲0,因此畫面還是因爲沒有計算系統欄的高度被遮蓋了一半。

該height=0應該是此時還沒被UI線程更新和賦值。

解決方法是將獲取系統欄和狀態欄的高度放在一個UI線程的handler裏面去排隊執行。

public class MainActivity extends Activity implements OnClickListener{

    LinearLayout rl;
    View topView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        
        // activity_main ID
        rl = (LinearLayout)findViewById(R.id.rl);
        
        topView = getWindow().getDecorView();
        rl.setPadding(0, 0, 0, 0);
        Button btOn = (Button) findViewById(R.id.on);
        btOn.setOnClickListener(this);
        
        Button btOff = (Button) findViewById(R.id.off);
        btOff.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch(item.getItemId()){
        case android.R.id.home:
            finish();
            break;
        }
        
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
        case R.id.on:
            hideSystemUI(topView);
            rl.setPadding(0, 0, 0, 0);
            break;
        case R.id.off:
            showSystemUI(topView);
            topView.postInvalidate();
            // 放到UI線程的handler裏面執行
            Message msg = new Message();
            msg.what = 1;
            this.validateHandler.sendMessage(msg);
            
            break;
        default:
            break;
        }
        
    }
    
     private Handler validateHandler = new  Handler() {  
        
            @Override  
            public void handleMessage(Message msg)  {  
                switch (msg.what)  
                {  
                case 1:  
                    Rect frame = new Rect();
                    topView.getWindowVisibleDisplayFrame(frame);
                    int paddingTop = getActionBar().getHeight()+ frame.top;
                    rl.setPadding(0, paddingTop, 0, 0);
                    break;  
                }  
                super.handleMessage(msg);  
            }  
        };  

      public static void hideSystemUI(View view) {
                view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                           | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                           | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                           | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                           | View.SYSTEM_UI_FLAG_FULLSCREEN
                           | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
     }
           
       @SuppressLint("NewApi")
       public static void showSystemUI(View view) {
           view.setSystemUiVisibility(
                   View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                   | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                   | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
       }
       

       @Override
       public boolean onCreateOptionsMenu(Menu menu) {
           // Inflate the menu; this adds items to the action bar if it is present.
           getMenuInflater().inflate(R.menu.main, menu);
           return true;
       }
}

由該博主後來回答,其實還可以通過反射方法直接從系統中取出actionbar和Statusbar的高度,也是一個很好的方法,這裏引用一下。

    // 獲取手機狀態欄高度
public static int getStatusBarHeight(Context context) {
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0, statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return statusBarHeight;
}
 
// 獲取ActionBar的高度
public static int getActionBarHeight(Context context) {
    TypedValue tv = new TypedValue();
    int actionBarHeight = 0;
    if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
    {
        actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
    }
    return actionBarHeight;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章