【问题标题】:How to remove Notification Bar shadow on the default Android Activity如何删除默认 Android Activity 上的通知栏阴影
【发布时间】:2015-12-18 11:06:02
【问题描述】:

尝试更改 Android Studio 自动生成的“空白活动”以不在通知栏上创建阴影。每次我创建它时,它似乎都会向主题文件添加代码,并且不可删除。

顺便说一句,我正在构建一个自定义 Appbar,所以我需要一个 NoActionbar 主题。

任何想法都很棒,谢谢!

NextActivity.java

public class NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

activity_next.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.soletrade.simpletest.NextActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_next" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

Android 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xxxxxxx.simpletest">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
        </activity>
        <activity
            android:name=".NextActivity"
            android:label="@string/title_activity_next"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

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" />

</resources>

【问题讨论】:

  • 在你的 oncreate 方法中使用这个 getSupportActionBar().hide();

标签: android android-theme shadow


【解决方案1】:

要移除阴影,请在您的 AppBarLayout 中添加 app:elevation="0dp"

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay" 
    app:elevation="0dp" >

或者您可以在 AppBarLayout 中添加 android:id 以在 java 类中删除

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay" >

然后在java类中

private AppBarLayout appBar;
.......
appBar = (AppBarLayout) findViewById(R.id.app_bar_layout);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    appBar.setElevation(0);
}

【讨论】:

    【解决方案2】:

    Android Studio 也会生成style.xml (v21),你只需删除&lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt;,通知栏上的阴影就会消失(覆盖)。

    另一种方法是在AppBarLayout中添加app:elevation="0dp",但是它会删除下面的阴影。

    【讨论】:

      猜你喜欢
      • 2015-04-13
      • 1970-01-01
      • 2021-02-24
      • 2013-10-03
      • 1970-01-01
      • 2020-07-09
      • 1970-01-01
      • 2014-12-21
      • 2023-03-27
      相关资源
      最近更新 更多