Gle*_*mut 65 android material-design android-toolbar
这是自我问答帖 我有透明的ActionBar覆盖布局.迁移到最新的支持库后,我被迫摆脱了ActionBar,转而使用工具栏.使其透明并覆盖该布局的旧方法不再适用.
<style name="CustomActionBarTheme" parent="@android:style/Theme.AppCompat">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:actionBarStyle">@style/TransparentActionBar</item>
</style>
<style name="TransparentActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
<item name="android:background">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Gle*_*mut 39
您只需要定义隐藏操作栏的主题,使用透明背景定义操作栏样式并将此样式设置为工具栏小部件.请注意,工具栏应作为最后一个视图绘制(在所有视图树上)
<style name="Theme.Custom" parent="@android:style/Theme.AppCompat">
<item name="windowActionBar">false</item>
<item name="windowActionBarOverlay">true</item>
<item name="android:windowActionBarOverlay">true</item>
</style>
<style name="CustomActionBar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<!-- Support library compatibility -->
<item name="windowActionBarOverlay">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Toolbar should be above content-->
<include layout="@layout/toolbar" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
工具栏布局:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:theme="@style/CustomActionBar"/>
Run Code Online (Sandbox Code Playgroud)
Tra*_*nVo 39
使用AppBarLayout的背景创建您的toolbar.xml 文件是@null
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="@+id/general_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Login"
android:textSize="20sp"/>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
这是结果:
Ped*_*ela 35
检查Google的示例源代码我发现了如何使工具栏完全透明.它比我想象的要简单.我们只需要像这样创建一个简单的Shape drawable.
drawable的名字是 toolbar_bg
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<gradient
android:angle="90"
android:startColor="@android:color/transparent"
android:endColor="@android:color/transparent"
android:type="linear" />
</shape>
Run Code Online (Sandbox Code Playgroud)
然后在片段或活动中..添加这样的工具栏.
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="@drawable/toolbar_bg"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
Run Code Online (Sandbox Code Playgroud)
在这里,我们将有一个完全透明的工具栏.
不要添加,<android.support.design.widget.AppBarLayout >如果你这样做,这将无法正常工作.
注意:如果需要AppBarLayout,请将高程设置为0,以便不绘制阴影.
小智 22
工具栏的工作方式与View类似,所以答案非常简单.
toolbar.getBackground().setAlpha(0);
Run Code Online (Sandbox Code Playgroud)
Rep*_*tor 13
在style.xml中添加以下行
<style name="Base.Theme.AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme" parent="Base.Theme.AppTheme">
</style>
Run Code Online (Sandbox Code Playgroud)
现在在style-v21中添加以下行,
<style name="AppTheme" parent="Base.Theme.AppTheme">
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
并将主题设置为android:theme ="@ style/AppTheme"
它将使状态栏透明.
oxi*_*ied 13
只有这对我有用(AndroidX 支持库):
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:theme="@style/AppTheme.AppBarOverlay"
android:translationZ="0.1dp"
app:elevation="0dp">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@null"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
此代码删除所有必要视图中的背景,并从 AppBarLayout 中删除阴影(这是一个问题)
答案在这里找到:删除 AppBarLayout 小部件 android 下面的阴影
只需android:background="@android:color/transparent" 在应用栏布局中添加以下内容
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>`
Run Code Online (Sandbox Code Playgroud)
小智 7
只需使用以下xml代码:
布局代码:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/def_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="@color/toolbarTransparent"
android:layout_alignParentTop="true" />
Run Code Online (Sandbox Code Playgroud)
并在colors.xml中添加以下代码:
<color name="toolbarTransparent">#00FFFFFF</color>
Run Code Online (Sandbox Code Playgroud)
这将为您提供所需的输出.
我通过创建两个Theme.AppCompat.Light.NoActionBar主题并将colorPrimary属性设置为透明颜色来实现半透明工具栏.
1)创建两个主题为不透明工具栏创建一个主题:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Toolbar background color -->
<item name="colorPrimary">#ff212cff</item>
</style>
Run Code Online (Sandbox Code Playgroud)
透明/叠加工具栏的第二个主题:
<style name="AppTheme.Overlay" parent="AppTheme">
<item name="colorPrimary">@color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
2)在您的活动布局中,将工具栏放在内容后面,以便它可以显示在它前面:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/container"
android:name="com.test.CustomFragment"
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="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
3)在AndroidManifest.xml中将透明主题应用于您的活动
<activity
android:name="com.test.TransparentActivity"
android:parentActivityName="com.test.HomeActivity"
android:theme="@style/AppTheme.Overlay" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.test.HomeActivity" />
</activity>
Run Code Online (Sandbox Code Playgroud)
也许您需要使用AppCompat-v7 21看一下这篇透明的操作栏
该帖子建议的要点是
这应该可以轻松解决问题。
小智 5
把透明工具栏的最简单方法是定义一个不透明@colors部分,定义在TransparentTheme @styles部分,然后把这些定义你的工具栏中即可.
@ colors.xml
<color name="actionbar_opacity">#33000000</color>
Run Code Online (Sandbox Code Playgroud)
@ styles.xml
<style name="TransparentToolbar" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<item name="android:windowActionBarOverlay">true</item>
<item name="windowActionBarOverlay">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
@ activity_main.xml中
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:background="@color/actionbar_opacity"
app:theme="@style/TransparentToolbar"
android:layout_height="?attr/actionBarSize"/>
Run Code Online (Sandbox Code Playgroud)
这就是结果:
| 归档时间: |
|
| 查看次数: |
115353 次 |
| 最近记录: |