在android中自定义标题栏默认值

时间:2013-06-02 09:08:09

标签: android layout titlebar

当我创建任何应用程序时,Android总是给我相同的标题栏。我需要做些什么来自定义它(背景和文字)?也许更改style.xml或更改onCreate()方法?

我更新了AndroidManifest.xml和styles.xml:

@的AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.searchmedicinelayout"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.app.searchmedicinelayout.SearchMedicineActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DetailMedicine"
            android:label="@string/detail"
            android:parentActivityName="com.app.searchmedicinelayout.SearchMedicineActivity" >

        </activity>
    </application>

</manifest>

@ styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
    </style>

</resources>

2 个答案:

答案 0 :(得分:1)

您可以在AndroidManifest.xml文件中更改应用程序和活动的标题。您使用自定义文本更改字段“android:label”的值。 “@ string / app_name”是在“strings”文件中声明的标签(projetc的文件夹“values”)。在那里,您可以创建或修改新标签。

  <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:name="shooterfugio.aquiniela2.MiApp">
    <activity
        android:name="shooterfugio.aquiniela2.MiTareaAsincrona"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.LoginActivity"
              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              android:label="@string/app_name" />
    <activity android:name=".Main"
              android:label="@string/app_name" />
    <activity android:name=".Aquiniela"
              android:label="@string/app_name" />
    <activity android:name=".IntroduceTexto"
              android:label="@string/app_name" />
    <activity
        android:name=".Popup1"
        android:label="@string/info"
        android:theme="@android:style/Theme.Dialog" >
    </activity>  
    <meta-data android:name="com.facebook.sdk.ApplicationId" 
               android:value="@string/applicationId" />
</application>

要更改背景,您必须在文件name_activity.xml中执行此操作,如下所示:

  <LinearLayout android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:layout_gravity="center_horizontal"
              android:gravity="center_horizontal"
              android:background="@drawable/image_background"
              android:id="@+id/main_ui_container">

注意:“image_background”是带有背景图片的文件的名称(位于“drawable”文件夹中)

答案 1 :(得分:0)

我可能需要查看您的代码,但标题栏的标题和样式通常在AndroidManifest.xml中定义。更具体地说,您想要查看定义应用程序的部分:

<application
 android:icon="@drawable/icon" 
 android:label="@string/app_name"
 android:theme="@style/customTheme">

现在,要更改标题栏的文本,您必须转到strings.xml并编辑app_name的值。另一方面,如果你想重新设置它,你需要在res / values /下创建一个style file,并使用一些样式xml。例如,以下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <style name="customTheme" parent="android:Theme.Light">
    <item name="android:windowBackground">#1BE032</item>
    <item name="android:colorBackground">#1BE032</item>
    </style>
    </resources>

会使窗口看起来有点太绿了。

在任何情况下,如果您使用的话,发布您的AndroidManifest和任何自定义主题会很有帮助。

相关问题