Android 如何获取支持操作栏的 AppCompat.Translucent 类型主题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20862258/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:36:44  来源:igfitidea点击:

Android how to get AppCompat.Translucent type theme with support actionbar?

androidandroid-actionbarandroid-themeandroid-actionbar-compat

提问by Edmund Rojas

I would like to add the support actionbar to one of my activities, I previously had been using the theme.translucent with this activity but in order to make the support actionbar work I needed to inherit the Theme.AppCompat, I need to maintain a translucent theme in this activity but unfortunately there isnt a Theme.AppCompat.translucent that i can see by default, is there any way that this can be done?

我想将支持操作栏添加到我的一个活动中,我以前一直在此活动中使用 theme.translucent 但为了使支持操作栏工作,我需要继承 Theme.AppCompat,我需要保持半透明此活动中的主题,但不幸的是没有默认情况下我可以看到的 Theme.AppCompat.translucent,有什么办法可以做到这一点?

回答by Cameron Ketcham

You can create a new set of styles to use which have the same properties as Theme.Translucentfrom themes.xml.

您可以创建一组新的样式来使用,这些样式Theme.Translucentthemes.xml 中的属性具有相同的属性。

Add the following to your styles.xml file:

将以下内容添加到您的 styles.xml 文件中:

<style name="Theme.AppCompat.Translucent">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

You can change the prefix Theme.AppCompatto something else if you want to inherit other things from the theme such as dialog styles etc. For example, a name like Theme.AppCompat.Light.Translucentwould have the properties of the Light theme.

Theme.AppCompat如果您想从主题继承其他内容(例如对话框样式等),您可以将前缀更改为其他内容。例如,名称 likeTheme.AppCompat.Light.Translucent将具有 Light 主题的属性。

To use the new style, set the theme property to @style/Theme.AppCompat.Translucent

要使用新样式,请将主题属性设置为 @style/Theme.AppCompat.Translucent

<activity
    android:name=".TranslucentActivity"
    android:theme="@style/Theme.AppCompat.Translucent" >
</activity>

回答by Rinku Bhilota

Parama ,

帕拉玛

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:colorBackgroundCacheHint">@null</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowAnimationStyle">@android:style/Animation</item>
    </style>

This should be the style header if you want the toolbar to disappear.you can use any parent theme which has NoActionBar for other effects.

如果您希望工具栏消失,这应该是样式标题。您可以使用任何具有 NoActionBar 的父主题来实现其他效果。

Hope this helps

希望这可以帮助

回答by Monika

If we use Translucent for transparent activity. It raises other issues - the color of Msgbox (now white previously black), Default dialog color, the spinners do drop down but do not show the underline and drop-down arrow. The spinners are color black text black; drop-down white drop-down text black and etc. To overcome this problem, you can just use below code

如果我们使用 Translucent 进行透明活动。它引发了其他问题 - Msgbox 的颜色(现在是白色以前是黑色)、默认对话框颜色、微调器确实下拉但不显示下划线和下拉箭头。微调器颜色为黑色,文字为黑色;下拉白色下拉文本黑色等。为了克服这个问题,你可以使用下面的代码

In style

很有型

<style name="Theme.AppCompat.Transparent.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
 <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">false</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

In manifest file

在清单文件中

<activity
        android:name=".activity.YourActivityName"
        android:theme="@style/Theme.AppCompat.Transparent.NoActionBar" />

I hope it will help Thanks

我希望它会有所帮助谢谢

回答by Monika

Cameron's answer is a nice hack, but it produced a floating action bar and tinted my status bar, which i didn't wanted . So i added more xml attributes for making status bar transparent(for sdk >=19) and used java code for making action bar invisible.

Cameron 的回答是一个不错的 hack,但它产生了一个浮动的操作栏并为我的状态栏着色,这是我不想要的。所以我添加了更多的 xml 属性来使状态栏透明(对于 sdk >=19)并使用 java 代码使操作栏不可见。

mainActivity.java :

mainActivity.java :

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().hide();
     ...
    }
     ...
}

styles.xml

样式文件

<style name="AppTheme.TranslucentBG">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>

</style>

manifest.xml

清单文件

<application
    android:icon="@mipmap/ic_launcher"
    ...
    android:theme="@style/AppTheme"
    ...
    >


    <activity android:name=".MainActivity"
        android:theme="@style/AppTheme.TranslucentBG"
        ...
        >
        ...
    </activity>
</application>