【发布时间】:2011-12-07 07:37:23
【问题描述】:
我为我的应用程序设计了一个自定义标题栏。它对我也很有效。但有一个问题。在我的自定义标题栏覆盖它之前,会看到默认标题(仅一秒钟)。有没有办法禁用android提供的默认标题栏?
我的代码就像...
res/layout 中的window_title.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="custom title bar"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
android:gravity ="center"/>
res/values 中的color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="customTheme" parent="android:Theme">
<item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground </item>
</style>
</resources>
res/values 中的styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="titlebackgroundcolor">#303010</color>
<color name="titletextcolor">#FFFF00</color>
</resources>
res/values 中的themes.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="WindowTitleBackground">
<item name="android:background">@color/titlebackgroundcolor</item>
</style>
</resources>
我还更新了 manifest.xml 的活动,因为
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.title"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:theme="@style/customTheme"
android:label="@string/app_name" >
<activity
android:name=".CustomTitleActivity"
android:theme="@style/customTheme" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在我的 onCreate 方法上,我做了类似的事情。
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title);
final TextView myTitle = (TextView) findViewById(R.id.myTitle);
myTitle.setText("Converter");
这也很好用
我已经编码为http://www.edumobile.org/android/android-programming-tutorials/creating-a-custom-title-bar/
而且效果也很好..
是否有禁用首先出现的默认标题?
【问题讨论】:
-
只是一个偶然的想法,您是否尝试将“setContentView”作为onCreate方法的最后一行?
-
是否需要编写自定义标题,如果没有然后将这一行 android:text="custom title bar" 更改为 android:text=""
-
@STT LCU & user370305:我已经试过了。当我编码时, getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.window_title);设置内容视图(R.layout.main);它显示错误。但是,如果我按照我之前所做的那样说,效果很好
-
@kelsi:实际上我必须在其中添加一个标签和 2 个图标。在我做复杂的事情之前,我只是想得到一个想法。
-
为 textview 尝试 android:layout_height="wrap_content"