【问题标题】:Android:Status bar color changes when searchview is triggeredAndroid:触发searchview时状态栏颜色发生变化
【发布时间】:2016-09-26 11:48:38
【问题描述】:

我正在我的应用中实现 searchview。当我单击搜索时,新活动打开以显示结果,但状态栏的颜色在新活动中发生变化。这是我的代码:

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>

样式.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>

这里是图片

【问题讨论】:

    标签: android android-layout searchview android-styles


    【解决方案1】:

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

    &lt;item name="android:statusBarColor"&gt;@android:color/transparent&lt;/item&gt;

    状态栏颜色由 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

    (这是我的第一个答案,希望对您有所帮助)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-28
      • 2017-03-05
      • 1970-01-01
      • 2015-11-20
      • 2021-12-11
      • 2013-09-16
      • 2014-06-23
      • 1970-01-01
      相关资源
      最近更新 更多