Android: Custom Title Bar

不羁岁月 提交于 2019-12-17 06:11:08

问题


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?


回答1:


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>



回答2:


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>



回答3:


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);

}



回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/3157522/android-custom-title-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!