0

我一直在尝试在我的活动中使用自定义标题栏。以下是我使用的代码

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    if ( customTitleSupported ) {
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
    }
    final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);
    if ( txtTitle != null ) {
        txtTitle.setText("PikMyBox - Welcome to PikMyBox");
    }
    setContentView(R.layout.activity_main);
}

custom_title_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtTitle"
android:layout_alignParentLeft="true"/>
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtCustomText"
    android:layout_alignParentRight="true"/>
</RelativeLayout>

样式.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
   <item name="colorPrimary">@color/colorSplash</item>
    <item name="android:windowNoTitle">true</item>
</style>

清单.xml

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    -----

执行活动时出现以下错误

android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                  at android.app.ActivityThread.access$800(ActivityThread.java:166)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5590)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                  at dalvik.system.NativeStart.main(Native Method)
               Caused by: android.util.AndroidRuntimeException: You cannot combine custom titles with other title features
                  at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:302)
                  at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2975)
                  at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3241)
                  at com.android.internal.policy.impl.PhoneWindow.getDecorView(PhoneWindow.java:1821)
                  at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:363)
                  at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
                  at android.support.v7.app.AppCompatDelegateImplV7.findViewById(AppCompatDelegateImplV7.java:229)
                  at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
                  at com.kommlabs.pikmybox.MainActivity.onCreate(MainActivity.java:25)
                  at android.app.Activity.performCreate(Activity.java:5447)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:166) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:136) 
                  at android.app.ActivityThread.main(ActivityThread.java:5590) 
                  at java.lang.reflect.Method.invokeNative(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:515) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                  at dalvik.system.NativeStart.main(Native Method) 

我怎么解决这个问题 ?

4

2 回答 2

0

在您的styles.xml 中更改android:windowNoTitle为。false

于 2016-12-21T05:12:44.663 回答
0

setContentView(R.layout.activity_main);在下面,final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);即为什么您会收到异常。您正在创建视图之前对其进行初始化。

将其更改为

 setContentView(R.layout.activity_main);
 final TextView txtTitle = (TextView) findViewById(R.id.txtTitle);

此外,如果您的自定义标题栏出现异常,请使用此样式

 <style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">false</item>
</style>

正如这里建议的那样https://stackoverflow.com/a/27410003/3111083

于 2016-12-21T05:46:45.023 回答