android透明状态栏

来源:互联网 发布:深入浅出java pdf云盘 编辑:程序博客网 时间:2024/04/30 08:32


miui v7 android4.4.4效果                                                          android 6.0 效果

      


基类Activity

package com.example.sss;import android.graphics.Color;import android.os.Build;import android.os.Bundle;import android.support.annotation.LayoutRes;import android.support.v4.app.FragmentActivity;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.view.Window;import android.view.WindowManager;import android.widget.FrameLayout;public class SupportActivity extends FragmentActivity  {@Overrideprotected void onCreate(Bundle arg0) {super.onCreate(arg0); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {  Window window = getWindow();          // Translucent status bar          window.setFlags(                  WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                  WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);setStatusBarColor(0);  }}/** * 设置状态栏颜色 * @param color   0 黑色 */    protected  void setStatusBarColor(int color){        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {             if (color==0){             getWindow().getDecorView().setBackgroundColor(Color.BLACK);             }else {                getWindow().getDecorView().setBackgroundColor(getResources().getColor(color));            }        }    }    @Override    public void setContentView(@LayoutRes int layoutResID) {            FrameLayout container=new FrameLayout(this);        //在用户布局外面嵌套一个FrameLayout,防止用户布局中根控件的margin失效        LayoutInflater.from(this).inflate(layoutResID,container,true);        setContentView(container);    }    @Override    public void setContentView(View view) {        super.setContentView(view);        adjustContentMargin(view);    }    private void adjustContentMargin(View view){         ViewGroup.LayoutParams param=view.getLayoutParams();        ViewGroup.MarginLayoutParams margParam;        if (param instanceof ViewGroup.MarginLayoutParams){            margParam=(ViewGroup.MarginLayoutParams) param;            margParam.setMargins(margParam.leftMargin,margParam.topMargin+getStatusBarHeight(),margParam.rightMargin,margParam.bottomMargin);        }else{            margParam=new ViewGroup.MarginLayoutParams(param);            margParam.setMargins(0,getStatusBarHeight(),0,0);        }        view.setLayoutParams(margParam);    }    protected int getStatusBarHeight() {        int result = 0;        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");        if (resourceId > 0) {            result = getResources().getDimensionPixelSize(resourceId);        }        return result;    }}

使用

package com.example.sss;import android.annotation.SuppressLint;import android.os.Bundle;public class MainActivity extends SupportActivity {@SuppressLint("ResourceAsColor")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);setStatusBarColor(R.color.colorPrimary);}}

布局

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity"    android:background="#fff"    >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="48dp"        android:background="@color/colorPrimary"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="标题"            android:textSize="20sp"            android:textColor="#fff"            android:layout_centerInParent="true"/>        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="返回"            android:layout_centerVertical="true"            android:textColor="#fff"            />    </RelativeLayout>    <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_gravity="center"        android:gravity="center"        android:text="hello" /></LinearLayout>

颜色资源

<?xml version="1.0" encoding="utf-8"?><resources>    <color name="colorPrimary">#18b4ed</color>    <color name="colorPrimaryDark">#18b4ed</color>    <color name="colorAccent">#FF4081</color>    <color name="colorRed">#ff003b</color></resources>

注意,要设置没有标题栏,可以在activity中设置,也可以在xml中配置:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.sss"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="11"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

values文件夹中styles.xml

<resources>    <!--        Base application theme, dependent on API level. This theme is replaced        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.    -->    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">        <!--            Theme customizations available in newer API levels can go in            res/values-vXX/styles.xml, while customizations related to            backward-compatibility can go here.        -->    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style></resources>





上面的方案中键盘弹出时可能导致布局上移,但从状态栏依旧能看到布局,采用下面方案可解决

package com.example.imageloader;        import android.graphics.Color;        import android.os.Build;        import android.os.Bundle;        import android.support.annotation.LayoutRes;        import android.support.v4.app.FragmentActivity;        import android.view.LayoutInflater;        import android.view.View;        import android.view.ViewGroup;        import android.view.Window;        import android.view.WindowManager;        import android.widget.FrameLayout;public class SupportActivity extends FragmentActivity  {    private View statusBar;    @Override    protected void onCreate(Bundle arg0) {        super.onCreate(arg0);        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            Window window = getWindow();            // Translucent status bar            window.setFlags(                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            statusBar=new View(this);            ViewGroup.LayoutParams param=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight());            statusBar.setLayoutParams(param);            setStatusBarColor(0);        }    }    /**     * 设置状态栏颜色     * @param color   0 黑色     */    protected  void setStatusBarColor(int color){        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            if (color==0){                statusBar.setBackgroundColor(Color.BLACK);            }else {                statusBar.setBackgroundColor(getResources().getColor(color));            }        }    }    @Override    public void setContentView(@LayoutRes int layoutResID) {        FrameLayout container=new FrameLayout(this);        //在用户布局外面嵌套一个FrameLayout,防止用户布局中根控件的margin失效        LayoutInflater.from(this).inflate(layoutResID,container,true);        setContentView(container);    }    @Override    public void setContentView(View view) {        super.setContentView(view);        adjustContentMargin(view);        ((ViewGroup)getWindow().getDecorView()).addView(statusBar);    }    private void adjustContentMargin(View view){        ViewGroup.LayoutParams param=view.getLayoutParams();        ViewGroup.MarginLayoutParams margParam;        if (param instanceof ViewGroup.MarginLayoutParams){            margParam=(ViewGroup.MarginLayoutParams) param;            margParam.setMargins(margParam.leftMargin,margParam.topMargin+getStatusBarHeight(),margParam.rightMargin,margParam.bottomMargin);        }else{            margParam=new ViewGroup.MarginLayoutParams(param);            margParam.setMargins(0,getStatusBarHeight(),0,0);        }        view.setLayoutParams(margParam);    }    protected int getStatusBarHeight() {        int result = 0;        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");        if (resourceId > 0) {            result = getResources().getDimensionPixelSize(resourceId);        }        return result;    }}


0 0
原创粉丝点击