当前位置: 首页 > 知识库问答 >
问题:

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

文华美
2023-03-14

我跟随一些在线示例实现了一个导航抽屉操作栏半透明状态栏。半透明的状态栏根本不会变为透明。

这是我的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/风格。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>

主要活动。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 -->

以及主要的活动。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);
}

}

共有1个答案

濮翰学
2023-03-14

改变这个

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

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

这将透明你的状态栏。

 类似资料:
  • 我使用以下布局(main_activity.xml) 具有以下样式-v21 但我仍然无法归档一个透明的状态栏 看起来像这样 但我想要这样的东西(新谷歌报摊App) 注意:我正在使用

  • 问题内容: 我有以下几行: 我将其放在MouseAdapter中的mouseReleased方法中。 基本上,我想使标签在单击时以半透明的绿色突出显示。 我在面板中有几个标签,所有标签都添加了此MouseAdapter。 我的问题是这样的: -当我单击标签时,它显示半透明的绿色,但是显示的是ANOTHER JLabel的背景,而不是我单击的背景。 无论我单击哪个标签,它始终会绘制同一标签的背景。

  • 当我发布Android4.0-4.3的笔记应用程序时,我使用了带有自定义操作栏图标的自定义操作栏颜色(而不是使用标准的浅色和深色操作栏)。我想在Android4.4上做到这一点,状态栏也将采用我在操作栏中使用的自定义颜色(这是#FFD060)。有没有一种方法可以轻松更改我当前的styles.xml,让4.4利用这一点? 我的风格。my values-v19文件夹下的xml如下所示: 我已尝试实施:

  • 我想整合一些像这样的东西: 我已经这样做了,但是我似乎不能把图像视图放在工具栏下面。没有工具栏,我可以在状态栏下制作,但是把这两者结合起来是不可能的。 这是我的布局: 在我的活动中,我做了以下工作: 我还声明了一个 styles-v21.xml 文件: 并将其设置为Photo活动的默认样式。我已经尝试将工具栏放在FrameLayout中,但这样做我的工具栏只会隐藏,如下所示: 提前感谢。 已修复,

  • 在“新”Android4.3版中有一个新功能。屏幕顶部的状态栏在launcher中是透明的(我在Galaxy S3上使用三星TouchWiz),在其他一些应用中也是透明的(看起来有点像iOS 7风格)。你能告诉我如何在我自己的应用程序(Eclipse、Java)中使状态栏透明(或彩色)吗?

  • 我的styles.xml: 我能做的是: