【问题标题】:Android Studio - Transparent Action Bar - Navigation DrawerAndroid Studio - 透明操作栏 - 导航抽屉
【发布时间】:2016-10-22 22:42:50
【问题描述】:

我有一个导航抽屉模板。我已经修改了操作栏,所以我看不到标题。而且我知道如何改变颜色,但我不知道如何获得这种效果:

这是我目前的状态:

这是我的styles.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="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

【问题讨论】:

    标签: android android-actionbar themes transparent android-theme


    【解决方案1】:

    您可以使用带有透明背景的Toolbar

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:id="@+id/your_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#4BAE4F"
            android:orientation="vertical">
    
            <!--Place your content here-->
    
        </LinearLayout>
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@android:color/transparent"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    
        </android.support.v7.widget.Toolbar>
    
    </RelativeLayout>
    

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        }
    }
    

    styles.xml

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
    
        </style>
    

    从 API 19 开始,您可以在 styles.xml 中设置参数 &lt;item name="android:windowTranslucentStatus"&gt;true&lt;/item&gt; 以使状态栏也透明。

    【讨论】:

    • 我试过了,但应用总是崩溃。有没有其他方法可以达到这种效果,还是我必须重新启动整个应用程序?
    • 查看 logcat 输出以了解崩溃的原因。还要确保将 AppTheme 的父级设置为 Theme.AppCompat.Light.NoActionBar
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    相关资源
    最近更新 更多