我为我的应用程序设计了一个自定义标题栏。它也适合我。但有一个问题。在我的自定义标题栏覆盖之前,可以看到默认标题(仅一秒钟)。有没有解决方案来禁用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/
而且效果也很好..
无论如何都要禁用首先出现的默认标题?
答案 0 :(得分: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>
然后在我的活动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
你不需要为标题栏设置任何其他内容,例如设置主题和设置其他功能或权限..........我希望你能得到它