【问题标题】:Struggling with toolbar using android support design library使用android支持设计库在工具栏上苦苦挣扎
【发布时间】:2015-08-01 01:15:04
【问题描述】:

我想在 RecyclerView 上获得透明的工具栏,其中包含我称之为 customHeader 的自定义相对布局。我在主要活动中的布局是这样的:

我使用android支持设计库v.22.2.1和其他相同版本的支持库 一切都很好,除了,我得到的工具栏是我的原色而不是透明的工具栏,尽管我已经从工具栏布局中删除了背景颜色。

这是我得到的结果:

我想要的是透明工具栏,但只有在我向上滚动时才应该填充原色

我的 activity_main 布局是:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           >
        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbarLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

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

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

        <FrameLayout
            android:id="@+id/content_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/btnCreate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="@dimen/btn_fab_margins"
            android:layout_marginRight="@dimen/btn_fab_margins"
            android:src="@drawable/share"
            app:borderWidth="0dp"
            app:elevation="6dp"
            app:pressedTranslationZ="12dp" />
    </android.support.design.widget.CoordinatorLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/vNavigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/WHITE"
        app:itemIconTint="@color/primary_text"
        app:itemTextColor="@color/primary_text"
        app:headerLayout="@layout/drawer_header"
        app:menu="@menu/drawer_menu"/>
</android.support.v4.widget.DrawerLayout>

那么我的片段中的布局是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Story list in main page -->
    <com.creativeLabs.news.util.MySwipeRefreshLayout
        android:id="@+id/swipe_refresh_story"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity$PlaceholderFragment">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_view_story_list"
            android:overScrollMode="never"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </com.creativeLabs.news.util.MySwipeRefreshLayout>
</RelativeLayout>

我的 customHeader 布局是:

<?xml version="1.0" encoding="utf-8"?>
<com.creativeLabs.news.ui.view.SlideTopStory
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_marginBottom="@dimen/story_list_item_margin_vertical"
    android:layout_width="match_parent"
    android:layout_height="@dimen/slide_image_height">

        <ImageView
            android:id="@+id/ivStoryimage"
            android:contentDescription="@string/story_title"
            android:scaleType="centerCrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"

            />

        <ProgressBar
            android:id="@+id/loading_bar"
            android:layout_centerInParent="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <!-- A mask view -->
        <View
            android:background="@drawable/title_slide_background"
            android:layout_alignParentBottom="true"
            android:layout_width="match_parent"
            android:layout_height="70dip"/>
        <View
            android:background="@drawable/title_slide_background1"
            android:layout_alignParentTop="true"
            android:layout_width="match_parent"
            android:layout_height="70dip"/>

        <TextView
            android:id="@+id/title"
            android:layout_alignParentBottom="true"
            android:textColor="@android:color/white"
            android:textSize="@dimen/text_size_large"
            android:gravity="center_vertical"
            android:paddingLeft="@dimen/slide_image_title_padding"
            android:paddingRight="@dimen/slide_image_title_padding"
            android:paddingEnd="@dimen/slide_image_title_padding"
            android:layout_marginBottom="@dimen/slide_image_title_margin"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

</com.creativeLabs.news.ui.view.SlideTopStory>

我的应用程序thes.xml:

  <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="colorControlNormal">@android:color/white</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    .......
    .......

in the manifest -> application tag:
android:theme="@style/AppTheme.WithoutActionBar">   

在我的活动和片段中,我没有编写任何关于工具栏颜色或除

之外的任何其他处理的代码
setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    toolbar.setNavigationIcon(getActionBarIconResource());

那么,为什么工具栏会获得应用程序的原色?我怎样才能得到一个透明的工具栏..有什么帮助吗? 在此先感谢

【问题讨论】:

    标签: android android-layout android-toolbar androiddesignsupport


    【解决方案1】:
    <android.support.v7.widget.Toolbar  
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light"  
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />   
    

    检查你的样式是如何应用的

    你在哪里设置为透明(alpha?)

    View.getBackground().setAlpha(..) ;
    

    【讨论】:

    • 我关注了这篇文章link,他刚刚删除了工具栏背景 (android:background="?attr/colorPrimary") 所以我想不需要将背景 alpha 设置为 0。什么是我的应用风格有误?
    • @user2198400 没有跟踪代码执行流程和完整源代码我不能说。尝试获取工具栏并设置 alpha 将满足您的所有需求 - 我指的是设置颜色透明(全 alpha)
    • 感谢您的贡献
    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2013-02-20
    • 1970-01-01
    相关资源
    最近更新 更多