0

我已经制作了自定义标题栏。现在我想添加标签。我已经实现了以下代码。但它显示了上述错误

这是我的主要活动课

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.activity_main_list);   
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.maintitlebar);
    loadPage();

      }

这是主要活动的布局文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".MainListActivity"
android:padding="0dp"
android:id="@+id/child_r1"
>

<ListView
    android:id="@android:id/list"
    android:layout_width="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:divider="#b5b5b5"
    android:dividerHeight="1dp"
    android:listSelector="@drawable/list_selector"
    android:layout_alignParentTop="true"
    android:transitionGroup="false"
    android:layout_height="wrap_content">

    </ListView>
</RelativeLayout>

这是我制作自己主题的 Style.xml 文件

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>
<style name="MyMaterialTheme" parent="MyMaterialTheme.Base">

</style>

<style name="MyMaterialTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="titleBarHeading">App for Blog</style>
<color name="colorPrimary">#125688</color>
<color name="colorPrimaryDark">#125688</color>
<color name="textColorPrimary">#FFFFFF</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#000000</color>
<color name="colorAccent">#c8e8ff</color>
</resources>

这是自定义标题栏的 Custom_theme.xml

<resources>
<style name="CustomWindowTitleBarBG">
    <item name="android:background">#323331</item>
</style>

<style name="TitleBarTheme" parent="android:Theme">
    <item name="android:windowTitleSize">35dip</item>
    <item name="android:windowTitleBackgroundStyle">
        @style/CustomWindowTitleBarBG</item>
    <item name="android:windowNoTitle">false</item>
</style>

</resources>

以下是android studio中的logcat错误

2732-2732/com.example.talha.appforblog E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.talha.appforblog, PID: 2732
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.talha.appforblog/com.example.talha.appforblog.MainListActivity}: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
 Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
        at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:309)
        at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:3244)
        at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3561)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:367)
        at android.app.Activity.setContentView(Activity.java:2144)
        at com.example.talha.appforblog.MainListActivity.onCreate(MainListActivity.java:82)
        at android.app.Activity.performCreate(Activity.java:5937)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:225            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)

           这是清单文件

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

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyMaterialTheme" > >
    <activity
        android:name=".MainListActivity"
        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=".ContentGetter"
        android:label="@string/title_activity_content_getter" >
    </activity>
    <activity
        android:name=".ColoumnView"
        android:label="@string/title_activity_coloumn_view" >
    </activity>
</application>

  

4

1 回答 1

2

您使用的主题 Theme.AppCompat.Light 包含此定义(在其父项中):

<item name="android:windowNoTitle">true</item>

所以主题说它没有标题,你很想使用自定义标题。这是不兼容的。

您使用的主题包含一个操作栏,并且操作栏中的标题的处理方式不同。

尝试这个:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
</style>
于 2015-11-08T20:02:42.330 回答