Android自定义工具栏重叠状态栏:如何解决它

时间:2015-11-17 10:34:43

标签: android android-layout

我正在使用Android app的项目。

我的代码如下:

我的 activity_mainmenu.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">
    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#ff8400">
        <ListView
            android:id="@+id/list_view_push"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <include android:id="@+id/toolbar" layout="@layout/include_toolbar"/>

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar">
        </android.support.v4.view.ViewPager>

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

我的工具栏

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:fitsSystemWindows="true"
    android:elevation="4dp"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/toolbar_title"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="left"
            android:gravity="center_vertical"
            android:text="@string/app_name"
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" />
        <TextView
            android:id="@+id/toolbar_audio"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:layout_marginRight="80dp"
            android:gravity="center_vertical"
            android:textColor="@color/background_gradient_start"
            android:text="@string/audio"
            android:visibility="gone"/>
        <TextView
            android:id="@+id/toolbar_video"
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_gravity="right"
            android:layout_marginRight="10dp"
            android:gravity="center_vertical"
            android:textColor="@color/background_gradient_start"
            android:text="@string/video"
            android:visibility="gone"/>
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Audio"
                android:layout_gravity="right"
                android:layout_marginRight="80dp"
                android:id="@+id/radioButtonAudio"
                android:checked="true"
                android:buttonTint="@color/background_gradient_start"/>
        <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text="Video"
                android:id="@+id/radioButtonVideo"
                android:layout_gravity="right"
                android:layout_marginRight="10dp"
                android:gravity="center_vertical"
                android:checked="false"
                android:buttonTint="@color/background_gradient_start"/>
    </FrameLayout>
</android.support.v7.widget.Toolbar>

AndroidManifest.xml

<application
        android:name="............AppController"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/UAmpAppTheme" >
        <!--This meta-data tag is required to use Google Play Services.-->
        <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" 
            android:screenOrientation ="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

使用 android:theme =“@ style / UAmpAppTheme”

<style name="UAmpAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowContentTransitions">true</item>

        <item name="colorPrimary">#ffff5722</item>
        <item name="colorPrimaryDark">#ffbf360c</item>
        <item name="colorAccent">#ffff5722</item>
    </style>

并且:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mainmenu);
    initializeToolbar();

....................

protected void initializeToolbar() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        if (mToolbar == null) {
            throw new IllegalStateException("Layout is required to include a Toolbar with id " +
                "'toolbar'");
        }
        mToolbar.inflateMenu(R.menu.main);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.open_content_drawer, R.string.close_content_drawer);
            mDrawerLayout.setDrawerListener(mDrawerListener);
            mDrawerLayout.setStatusBarBackgroundColor(ResourceHelper.getThemeColor(this, R.attr.colorPrimary, android.R.color.black));
            populateDrawerItems();
            setSupportActionBar(mToolbar);
            updateDrawerToggle();
        } else {
            setSupportActionBar(mToolbar);
        }  
            //int resourceIdnavigation_bar_height = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
            //if (resourceIdnavigation_bar_height > 0) {
                //LogHelper.d("MainMenu", " Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH  1 " + getResources().getDimensionPixelSize(resourceIdnavigation_bar_height));
            //};

            //int resourceIdstatus_bar_height = getResources().getIdentifier("status_bar_height", "dimen", "android");
           // if (resourceIdstatus_bar_height > 0) {
                //LogHelper.d("MainMenu", " Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH  2 " + getResources().getDimensionPixelSize(resourceIdstatus_bar_height));
           // }

        mToolbarInitialized = true;
    }

我的自定义操作工具栏有问题:

  • 状态栏下面的自定义工具栏重叠

如何解决它。

见图片:enter image description here

此问题发生在( Build.VERSION.SDK_INT&lt; Build.VERSION_CODES.KITKAT_WATCH

此问题不会发生在( Build.VERSION.SDK_INT&gt; = Build.VERSION_CODES.KITKAT_WATCH

见图片:enter image description here

由于

2 个答案:

答案 0 :(得分:0)

您是否尝试将app:layout_behavior="@string/appbar_scrolling_view_behavior"添加到相对布局?像这样。

 <?xml version="1.0" encoding="utf-8"?>
    <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">
        <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:background="#ff8400"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
            <ListView
                android:id="@+id/list_view_push"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

            <include android:id="@+id/toolbar" layout="@layout/include_toolbar"/>

            <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/pager"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/toolbar">
            </android.support.v4.view.ViewPager>

        </RelativeLayout>
    </android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

我刚刚找到了解决问题的方法。

现在,我将为每个人分享。

与标题一样: Android自定义工具栏重叠状态栏:如何解决

发生此问题 (Build.VERSION.SDK_INT&lt; Build.VERSION_CODES.KITKAT_WATCH)(android&lt; 4.4)

此问题不会发生 (Build.VERSION.SDK_INT&gt; = Build.VERSION_CODES.KITKAT_WATCH)

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true" <!-- remove this line -->
    tools:context=".MainActivity">
    <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fitsSystemWindows="true" <!-- add here -->
        android:background="#ff8400">
        <ListView
            android:id="@+id/list_view_push"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"/>

        <include android:id="@+id/toolbar" layout="@layout/include_toolbar"/>

        <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/toolbar">
        </android.support.v4.view.ViewPager>

    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

希望能帮助每个人

感谢您阅读