簡體   English   中英

使用自定義標題欄android的例外

[英]Exception on using custom title bar android

我一直在嘗試在活動中使用自定義標題欄。 以下是我使用的代碼

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>

Styles.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>

的Manifest.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) 

我怎么解決這個問題 ?

在styles.xml中將android:windowNoTitle更改為false

您的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM