【问题标题】:Customize title bar default in android [duplicate]在android中自定义标题栏默认值[重复]
【发布时间】:2013-06-02 09:08:08
【问题描述】:

当我创建任何应用程序时,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>

【问题讨论】:

    标签: android layout titlebar


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

    要改变背景,必须在文件名_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”文件夹中)

    【讨论】:

    • 我想你不明白我的问题。我想更改包含图标、标签、项目名称的标题栏...我只更改背景并修改文本大小、文本颜色、文本对齐方式。我不想在布局中显示它。
    【解决方案2】:

    我可能需要查看您的代码,但标题栏的标题和样式通常在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 和任何自定义主题(如果您正在使用任何主题)都会很有帮助。

    【讨论】:

    • 我只想更改标题栏上的背景并设置文本中心并更改大小。我创建项目时默认使用 AndroidManifest。我已经更新了上面的 2 个文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-22
    • 2011-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多