如何在android中使用自定义标题覆盖默认标题



我已经为我的应用程序设计了一个自定义标题栏。它对我也很有效。但是有一个问题。在我的自定义标题栏覆盖它之前,可以看到默认标题(只有一秒钟)。有没有办法禁用android提供的默认标题栏?

我的代码是…

window title.xml in res/layout

<?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"/>

color.xml in res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="customTheme" parent="android:Theme"> 
    <item name="android:windowTitleBackgroundStyle">@style/WindowTitleBackground </item>
    </style> 
</resources>

style .xml in res/values

<?xml version="1.0" encoding="utf-8"?>
<resources>   
    <color name="titlebackgroundcolor">#303010</color>
    <color name="titletextcolor">#FFFF00</color>
</resources>

themes.xml in res/values

<?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/而且效果也很好。

是否有办法禁用默认的标题,首先出现?

为什么你在android上使用标题栏呢?只要按照你的想法设计你的标题栏,并将其设置在主布局的顶部,并在设置布局之前使用onCreate中的以下代码

requestWindowFeature(Window.FEATURE_NO_TITLE);

这里有一个例子…在你的主布局中定义你的标题栏如果你将main。xml设置为你的activity的布局那么你的main。xml应该像

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
   //here i have used LinearLayout as example your's can be Relative or whatever
 //here my title bars layout starts containing an icon and a text your's can containing just text or whatever you want
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/my_title_background"
    android:orientation="horizontal" >
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dip"
        android:src="@drawable/my_icon" />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginLeft="15dip"
        android:text="My Application title"
        android:textColor="#FFFFFF" />
</LinearLayout>
   //and here rest of your layut for your application functionality
</LinearLayout>

然后在我的activity java文件中包含onCreate()方法代码将是这样的

 /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    //restof code for my class

你不需要为你的标题栏设置任何其他东西,如设置主题和设置其他功能或权限..........

最新更新