【问题标题】:how to override default title with custom title in android如何在android中用自定义标题覆盖默认标题
【发布时间】: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"

标签: android custom-titlebar


【解决方案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

你不需要为你的标题栏设置任何其他东西,比如设置主题和设置其他功能或权限............我希望你明白了

【讨论】:

  • 你能描述一下我是怎么做到的吗?我是安卓新手。只是在学习。我想我已经创建了我的标题栏。并且工作正常。你会检查我的代码并提出我错的地方吗?
  • 把上面那行放在你的 onCreate();
  • @April 和 sam.Janz:不,亲爱的……它也不起作用……我曾尝试将该代码放在我的 oncreate 中。当我这样做时,它会捕获错误
  • @sam.janz:我认为实际上你不是我的问题。我知道这是可能的,正如你所描述的......我是一名学生,想研究自定义标题的工作原理。所以只想知道我是如何工作的。谢谢亲爱的...
  • 我确信您的代码运行良好。但实际上现在我正在研究自定义标题..我希望对此有更多的了解......无论如何感谢你抽出时间来帮助我亲爱的
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-22
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多