Android:自定义标题栏

发布于 2024-09-08 00:19:37 字数 686 浏览 4 评论 0原文

我有一个自定义标题栏

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
 setContentView(R.layout.activities);
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

,基本上工作正常。问题是,在调用上面的代码之前,都会显示默认标题栏。我根本不想要标题栏,换句话说,在我的标题栏出现之前,不会出现任何标题。

将其添加到清单中:

<application android:theme="@android:style/Theme.NoTitleBar">

导致强制关闭。我的清单看起来像这样,

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:theme="@style/My_Theme">

我需要 my_Theme 的地方,因为它设置了背景颜色,在我的客户主题中设置背景颜色会导致我的彩色背景周围出现灰色区域。因此,即使没有强制关闭,我也不确定无标题是否有帮助。

有什么想法吗?

I have a custom title bar

 requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
 setContentView(R.layout.activities);
 getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);

Which works basically fine. The problem is that until the above code is called the default title bar is shown. I don't want a title bar there at all, in other words before mine shows up no title shall show up.

Adding this to the manifest:

<application android:theme="@android:style/Theme.NoTitleBar">

leads to a force close. My manifest looks like this

<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:theme="@style/My_Theme">

Where I need my_Theme since it sets the background color, setting the background color in my customer theme leads to a gray area around my colored backround. So even without the force close I am not sure if the no title will help.

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

往日情怀 2024-09-15 00:19:37

我和你有同样的问题。

问题出在你的风格上。

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="My_Theme">
        <item name="android:windowTitleSize">35dp</item>
        <item name="android:windowTitleBackgroundStyle">@android:color/black</item>
    </style>
</resources>

I had the same issue as you.

The issue is with something you have in your style.

Try this out:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="My_Theme">
        <item name="android:windowTitleSize">35dp</item>
        <item name="android:windowTitleBackgroundStyle">@android:color/black</item>
    </style>
</resources>
碍人泪离人颜 2024-09-15 00:19:37

这是我唯一的一个,它可以在我的自定义标题启动之前阻止默认标题:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="CustomWindowTitleStyle">
        <item name="android:textColor">@android:color/transparent</item>
    </style>

    <style name="CustomTheme" parent="@android:style/Theme.Holo">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowTitleBackgroundStyle">@android:color/transparent</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleStyle">@style/CustomWindowTitleStyle</item>
    </style>

</resources>

This one is the only one for me, which prevents the default-title before my custom title is initiated:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="CustomWindowTitleStyle">
        <item name="android:textColor">@android:color/transparent</item>
    </style>

    <style name="CustomTheme" parent="@android:style/Theme.Holo">
        <item name="android:windowActionBar">false</item>
        <item name="android:windowTitleBackgroundStyle">@android:color/transparent</item>
        <item name="android:windowTitleSize">50dp</item>
        <item name="android:windowTitleStyle">@style/CustomWindowTitleStyle</item>
    </style>

</resources>
烟─花易冷 2024-09-15 00:19:37

首先,如果您的应用程序有 NoTitleBar,为什么还要使用自定义标题栏?那太愚蠢了!

不用说,这是您的问题,您必须删除该标志。

无论如何,添加自定义标题栏的最佳方法是仅使用 xml。这可以避免您的应用程序重复加载标题栏;用户将会看到哪些内容。

../res/styles.xml

<resources>

   <style name="AppTheme parent="@style/android:Theme.Light">
      <item name="android:windowNoTitle">false</item>
      <item name="android:windowTitleSize">30dp</item
      <item name="android:windowTitleStyle">@layout/custom_title</item>
   </style>

</resources>

那么您不需要有关 requestAnything 的代码。

First thing why are you using a custom title bar if your app has NoTitleBar? That is silly!

Needless to say, this is your problem and you must remove that flag.

Anyhow, the best way to add custom title bar is in xml only. This avoids your app double loading the title bar; which users will see.

../res/styles.xml

<resources>

   <style name="AppTheme parent="@style/android:Theme.Light">
      <item name="android:windowNoTitle">false</item>
      <item name="android:windowTitleSize">30dp</item
      <item name="android:windowTitleStyle">@layout/custom_title</item>
   </style>

</resources>

Then you dont need that code about requestAnything.

蝶…霜飞 2024-09-15 00:19:37

您还应该检查它是否支持 customTitle。

Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);



if (customTitleSupported) {
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);

}

you should also check whether customTitle is supported by it or not.

Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.main);



if (customTitleSupported) {
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);

}
鹿! 2024-09-15 00:19:37

您的应用程序崩溃是因为在您的代码中您从窗口功能调用标题栏,而在另一侧您通过清单禁用它。基本上你不能这样做,它在逻辑上是错误的。
您需要修改标题栏而不是删除它。

Your App crashes because in your code you calling Title Bar from window features and in other side you disabling it through manifest. Basically You cant do this, its logically incorrect.
You need to modify your title bar not to remove it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文