无法显示半透明的Android状态栏



我在网上学习了一些例子来实现导航抽屉+操作栏+半透明状态栏。半透明的状态栏根本不会变成透明的。

这是我的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name="com.getbase.floatingactionbutton.sample.MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
</application>

v21/styles.xml

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
    <item name="android:colorPrimary">@color/ColorPrimary</item>
    <item name="android:colorPrimaryDark">@color/ColorPrimaryDark</item>
    <!-- Customize your theme here. -->
</style>
<style name="menu_labels_style">
    <item name="android:background">@drawable/fab_label_background</item>
    <item name="android:textColor">@color/simplify_blue</item>
    <item name="android:textStyle">bold</item>
</style>

activity_main.xml

<!-- Beginning of Navigation Drawer -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/DrawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <!-- Beginning of Floating Action Menu -->
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:fab="http://schemas.android.com/apk/res-auto"
        android:background="@color/background"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.Toolbar
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/tool_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            android:background="@color/ColorPrimary"
            app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">
        <com.getbase.floatingactionbutton.FloatingActionsMenu
            android:id="@+id/simplify_actions"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            fab:fab_addButtonColorNormal="@color/simplify_blue"
            fab:fab_addButtonColorPressed="@color/simplify_blue_pressed"
            fab:fab_addButtonPlusIconColor="@color/white"
            fab:fab_labelStyle="@style/menu_labels_style"
            android:layout_marginBottom="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginEnd="16dp">
            <com.getbase.floatingactionbutton.FloatingActionButton
                android:id="@+id/action_scan_qr"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                fab:fab_colorNormal="@color/white"
                fab:fab_title="SCAN QR CODE"
                fab:fab_size="mini"
                fab:fab_icon="@drawable/ic_scan_qr_code"
                fab:fab_colorPressed="@color/white_pressed"/>
            <com.getbase.floatingactionbutton.FloatingActionButton
                android:id="@+id/action_share_hotspot"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                fab:fab_colorNormal="@color/white"
                fab:fab_title="SHARE HOTSPOT"
                fab:fab_size="mini"
                fab:fab_icon="@drawable/ic_share_wifi"
                fab:fab_colorPressed="@color/white_pressed"/>
            <com.getbase.floatingactionbutton.FloatingActionButton
                android:id="@+id/action_discovery"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                fab:fab_colorNormal="@color/white"
                fab:fab_title="SEARCH NETWORK"
                fab:fab_size="mini"
                fab:fab_icon="@drawable/ic_discover_network"
                fab:fab_colorPressed="@color/white_pressed"/>
        </com.getbase.floatingactionbutton.FloatingActionsMenu>
        <!-- End of Floating Action Menu -->
        </RelativeLayout>
    </LinearLayout>
<LinearLayout
    android:layout_width="304dp"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    android:fitsSystemWindows="true">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:layout_gravity="left"
        android:background="#ffffff"
        android:scrollbars="vertical">
    </android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
<!-- End of Navigation Drawer -->

和MainActivity.java

public class MainActivity extends AppCompatActivity {
private boolean IS_HOTSPOT_ON = false;
String TITLES[] = {"Profile","Payment","History","Promotions","Usage","Speed","Settings"};
int ICONS[] = {R.drawable.ic_profile,R.drawable.ic_payment,R.drawable.ic_history,R.drawable.ic_promotion,R.drawable.ic_usage,R.drawable.ic_speed,R.drawable.ic_settings};
String NAME = "Yoona Im";
String EMAIL = "yoona@nextwave.my";
int PROFILE = R.drawable.yoona;
private Toolbar toolbar;
RecyclerView mRecyclerView;                           // Declaring   RecyclerView
RecyclerView.Adapter mAdapter;                        // Declaring Adapter For Recycler View
RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout Manager as a linear layout manager
DrawerLayout Drawer;                                  // Declaring DrawerLayout
ActionBarDrawerToggle mDrawerToggle;                  // Declaring Action Bar Drawer Toggle
@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

  toolbar = (Toolbar) findViewById(R.id.tool_bar);
  setSupportActionBar(toolbar);
  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
  getSupportActionBar().setHomeButtonEnabled(true);
  getSupportActionBar().setTitle(R.string.app_name);
  mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View
  mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size
  mAdapter = new MyAdapter(TITLES,ICONS,NAME,EMAIL,PROFILE);       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
  // And passing the titles,icons,header view name, header view email,
  // and header view profile picture
  mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView
  mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager
  mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager
  Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);        // Drawer object Assigned to the view
  mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close){

      @Override
      public void onDrawerOpened(View drawerView) {
          super.onDrawerOpened(drawerView);
          // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
          // open I am not going to put anything here)
      }
      @Override
      public void onDrawerClosed(View drawerView) {
          super.onDrawerClosed(drawerView);
          // Code here will execute once drawer is closed
      }
  }; // Drawer Toggle Object Made

final FloatingActionsMenu menuSimplifyActions = (FloatingActionsMenu) findViewById(R.id.simplify_actions);
final FloatingActionButton actionScanQR = (FloatingActionButton) findViewById(R.id.action_scan_qr);
  actionScanQR.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View view) {
      actionScanQR.setTitle("SCANNING CODE NOW...");
  }
});
final FloatingActionButton actionShareHotspot = (FloatingActionButton) findViewById(R.id.action_share_hotspot);
  actionShareHotspot.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View view) {
          if (!IS_HOTSPOT_ON) {
              actionShareHotspot.setIcon(R.drawable.ic_disable_hotspot);
              actionShareHotspot.setTitle("SWITCH OFF HOTSPOT");
          }
          else {
              actionShareHotspot.setIcon(R.drawable.ic_share_wifi);
              actionShareHotspot.setTitle("SHARE HOTSPOT");
          }
          IS_HOTSPOT_ON = !IS_HOTSPOT_ON;
      }
  });
  Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
  mDrawerToggle.syncState();               // Finally we set the drawer toggle sync State
}

@Override
public void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

更改此

 <item name="android:colorPrimaryDark">@color/ColorPrimaryDark</item>

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

这将使您的状态栏透明。

最新更新