6

我为我的应用程序设计了一个自定义标题栏。它对我也很有效。但有一个问题。在我的自定义标题栏覆盖它之前,会看到默认标题(仅一秒钟)。是否有任何解决方案可以禁用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 中的主题.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/ 它也很好用..

无论如何要禁用首先出现的默认标题?

4

1 回答 1

1

为什么要在 android 上中继标题栏只需根据需要设计标题栏并将其设置在主布局的顶部,并在设置布局之前在 onCreate 中使用以下代码

requestWindowFeature(Window.FEATURE_NO_TITLE);

这是示例...在主布局中定义标题栏,例如如果您将 main.xml 设置为活动的布局,那么您的 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>

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

 /** 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

您不需要为标题栏设置任何其他内容,例如设置主题和设置其他功能或权限............我希望你明白了

于 2011-12-07T08:32:04.967 回答