安卓菜单栏透明化---着色法

对于菜单栏的透明化在activity中的代码是很少的。主要操作布局文件和注册清单文件

网上把菜单栏的透明化分了两个版本,4.4以后,和5.0以后。下面我写的是5.0以后的。(亲测可用)


第一步:你的activity需要禁用掉标题栏。

方法:我是定义一个基类,里面主要是  supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

public class BaseActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
    }
}
第二步:在你的activity布局文件中自定义一个标题栏(这个我就略写了,直接用一个RelativeLayout布局代替

<RelativeLayout
    android:id="@+id/bbbbb"
    android:layout_width="match_parent"
    android:layout_height="55dp"
    android:background="#你自己定义的颜色">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="标题栏---"
        android:textColor="@android:color/white"
        android:textSize="20sp" />

</RelativeLayout>

第三步:在你的布局文件根布局中添加几行代码

1.  android:fitsSystemWindows="true"
2.  android:background="#你自己定义的颜色"  //上文你标题栏定义的颜色

第四步:自定义一个主题,在res目录下的value文件夹里面的style.xml文件

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!--菜单栏着色-->
    <style name="TransparentTheme" parent="AppTheme"></style>
</resources>

第五步:在配置清单文件中引用这个布局,也就是在配置清单文件里的 android:theme="@style/TransparentTheme"这个属性。







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