自定义标题栏和原始标题栏都显示

时间:2016-08-27 12:58:24

标签: android

我正在尝试创建自定义标题栏而不是android studio提供的标题栏。 我的activity_main.xml是:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.hsports.fragmentsinandroid.MainActivity">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:id="@+id/textTitle"
        android:text="CustomTitle"

        />


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/duck"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        />



</RelativeLayout>

我的MainActivity类是:

    package com.example.hsports.fragmentsinandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.activity_main);

        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.activity_main);
    }
}

我的style.xml是:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="CustomTheme" parent="@style/Theme.AppCompat">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:windowNoTitle">false</item>
    </style>
</resources>

我的androidManifest.xml文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hsports.fragmentsinandroid">

    <application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/CustomTheme">
<activity android:name=".MainActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
</application>

    </manifest>

使用上面的代码为android应用程序后,我得到截图中附加的输出。请看一下附带的截图。 我得到了两个标题栏(自定义+原创)。我想要只显示自定义的一个。 the attached screenshot is here.

1 个答案:

答案 0 :(得分:0)

你检查了这个cannot combine custom titles with other title features

您应该在自定义主题中使用<item name="android:windowNoTitle">false</item>

如果您想在主题中使用自定义标题,则需要禁用FEATURE_NO_TITLE