安卓隐藏状态栏

来源:互联网 发布:淘宝财务报表 编辑:程序博客网 时间:2024/05/18 00:57

以前为了美化界面,需要隐藏状态栏,但是每次隐藏后发现布局上串了,特别难看。最后我自己写一个方法来解决。

package com.neusoft.phone.xinhua;import android.content.Context;import android.os.Build;import android.text.Layout;import android.view.View;import android.view.WindowManager;import android.widget.LinearLayout;/** * Created by Administrator on 2016/11/26. */public class Util {        public static void setToolbar(Context context,View layout,View view)        {            int titleH = (int) context.getResources().getDimension(R.dimen.title_bar_height);            int toolbarH = getStatusBarHeight(context);            LinearLayout.LayoutParams viewParams = (LinearLayout.LayoutParams) view.getLayoutParams(); // 取控件mGrid当前的布局参数            viewParams.height = toolbarH;// 当控件的高强制设成状态栏高度            view.setLayoutParams(viewParams); // 使设置好的布局参数应用到控件view            LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) layout.getLayoutParams(); // 取控件mGrid当前的布局参数            layoutParams.height = toolbarH+titleH;// 当控件的高强制设成状态栏高度            layout.setLayoutParams(layoutParams); // 使设置好的布局参数应用到控件view        }        /*        * get toolbar height        */        public static int getStatusBarHeight(Context context) {            int result = 0;            int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");            if (resourceId > 0) {                result = context.getResources().getDimensionPixelSize(resourceId);            }            return result;        }}

下面为Activity(注意我传的参数)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);        getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);    }    setContentView(R.layout.report_activity_my_report);    Util.setToolbar(getApplicationContext(),findViewById(R.id.toolbarllayout),findViewById(R.id.toolbarview));
xml部分:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:id="@+id/toolbarllayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@drawable/report_title_bg"        android:orientation="vertical">        <TextView            android:id="@+id/toolbarview"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="@dimen/title_bar_height"            android:gravity="center_vertical" >            <TextView                android:id="@+id/topbar_title"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                android:text="@string/report_my_report"                android:textColor="@color/white"                android:textSize="@dimen/title_text_size" />            <ImageButton                android:id="@+id/back"                style="@style/friend_circle_title_icon_style"                android:layout_alignParentLeft="true"                android:layout_marginLeft="@dimen/title_icon_margin_left_and_right"                android:contentDescription="@null"                android:src="@drawable/btn_blue_left_selector" />            <ImageButton                android:id="@+id/add"                android:layout_width="@dimen/topic_my_topic_add_img_size"                android:layout_height="@dimen/topic_my_topic_add_img_size"                android:layout_centerVertical="true"                android:layout_alignParentRight="true"                android:layout_marginRight="@dimen/topic_new_topic_submit_text_margin_left_and_right"                android:background="@drawable/topic_report_add"  />        </RelativeLayout>    </LinearLayout></LinearLayout>

搞定!

0 0