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;    }}


更多相关文章

  1. android ComponentName 示例
  2. android 在线视频播放器实现方法
  3. Android(安卓)打开Activity后,不显示键盘
  4. 进度条及拖动条背景颜色设置(progressDrawable)
  5. Android(安卓)WebView总结
  6. Android不让弹出键盘挡住View
  7. android 如何在对话框中获取edittext中的数据
  8. Android(安卓)ViewPager动画第三方库(MagicViewPager)
  9. windowSoftInputMode属性设置值

随机推荐

  1. 卡片式UI的总结 android
  2. Android(安卓)Map API key 申请
  3. Google Android软件架构
  4. Android中的DownloadManager
  5. android 源码分析流程(一) init.c
  6. Android中String类型进行比较大小
  7. android 利用 git 信息区分 apk 版本
  8. android根据屏幕高度改变item占ListView
  9. Android(安卓)NDK开发method GetStringUT
  10. android页面全屏及状态栏和导航栏的(沉浸