Android:自定义标题栏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3157522/
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
Android: Custom Title Bar
提问by paradroid666
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.
我需要 my_Theme 的地方,因为它设置了背景颜色,在我的客户主题中设置背景颜色会导致我的彩色背景周围出现灰色区域。因此,即使没有强制关闭,我也不确定无标题是否会有所帮助。
Any idea?
任何的想法?
回答by Macarse
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>
回答by Martin L.
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>
回答by Jawad Zeb
you should also check whether customTitle is supported by it or not.
您还应该检查它是否支持 customTitle。
Boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
}
回答by Jawad Zeb
First thing why are you using a custom title bar if your app has NoTitleBar? That is silly!
第一件事,如果您的应用程序有 NoTitleBar,为什么要使用自定义标题栏?那是愚蠢的!
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.
无论如何,添加自定义标题栏的最佳方法仅在 xml 中。这可以避免您的应用程序重复加载标题栏;哪些用户会看到。
../res/styles.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>
Then you dont need that code about requestAnything.
那么你不需要关于 requestAnything 的代码。
回答by Jawad Zeb
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.
您的应用程序崩溃是因为在您的代码中,您从窗口功能调用了标题栏,而在另一方面,您通过清单禁用了它。基本上你不能这样做,它在逻辑上是不正确的。您需要修改标题栏而不是将其删除。