Android:Status 触发搜索视图时条形颜色发生变化

Android:Status bar color changes when searchview is triggered

我正在我的应用程序中实现搜索视图。当我点击搜索时,新 activity 打开以显示结果,但状态栏的颜色在新 activity 中发生变化。这是我的代码:

activity_search.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout    
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/coordinator_layout">

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

  <android.support.v7.widget.RecyclerView
    android:id="@+id/search_results_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
  </android.support.v7.widget.RecyclerView>

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

SearchResultActiviy.java

public class SearchResultActivity extends AppCompatActivity{

  private Toolbar toolbar;
  @Override
  protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    handleIntent(getIntent());
  }
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(getIntent());
}

private void handleIntent(Intent intent) {
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        query = intent.getStringExtra(SearchManager.QUERY);
        getPatientList(startRow,pageSize);
    }
}

AndroidManifest.xml

<activity android:name=".activities.SearchResultActivity"
    android:theme="@style/AppTheme.NoActionBar">
    <intent-filter>
        <action android:name="android.intent.action.SEARCH"/>
    </intent-filter>
    <meta-data
        android:name="android.app.searchable"
        android:resource="@xml/searchable" />
</activity>

Style.xml

<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>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:textAllCaps">false</item>
</style>

这是图片

在您的 Style.xml 中,您将 statusBarColor 定义为透明。 此参数需要 API 级别 21。 我建议删除行:

<item name="android:statusBarColor">@android:color/transparent</item>

状态栏的颜色由 ColorPrimaryDark 设置(参见https://developer.android.com/training/material/theme.html

如果你想动态改变颜色,使用

Window window = activity.getWindow();

// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);

// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

// finally change the color
window.setStatusBarColor(activity.getResources().getColor(R.color.my_statusbar_color));

在此线程中解释:statusBar

(第一次回答,希望对你有帮助)