[英]Android setting custom title bar error
我正在創建一個簡單的Android應用,我想在其中添加自定義標題欄。 但是,在創建自定義標題欄時遇到了一個問題。
這是我在GetPhoto Activity類中的OnCreate方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
boolean titled = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE
setContentView(R.layout.getphoto);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title_complex);
initialize();
}
和我的title_complex.xml(用於定義自定義標題):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:orientation="horizontal" >
<Imageview
android:src="@drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Hello world!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#7cfc00" />
</LinearLayout>
當我將getWindow()。setFeatureInt()放在setContentView()之后時,出現如下錯誤:
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.featureselection/com.example.featureselection.GetPhoto}: android.view.InflateException: Binary XML file line #16: Error inflating class Imageview
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
at android.app.ActivityThread.access$600(ActivityThread.java:127)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
...
featureselection是應用程序名稱,而GetPhoto是包含上面顯示的OnCreate方法的類。 我不知道為什么會出錯。 我看到其他人也寫了這樣的代碼,並且效果很好。
另一件事是,如果我將getWindow()。setFeatureInt()放在setContentView()之前,則在運行我的應用程序時,它不會給出錯誤,但是自定義標題欄上什么也沒有顯示,而我希望有圖像和“ hello world” ”顯示在自定義標題欄上。
僅供參考:我在res / values文件夾中也有這個customer_style.xml:
<resources>
<style name="CustomWindowTitleBackground">
<item name="android:background">#008000</item>
</style>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle">@style/CustomWindowTitleBackground</item>
</style>
</resources>
設置樣式。
有誰知道問題是什么以及如何解決? 任何隔離或解決方案均適用。
確保您的XML文件使用ImageView而不是Imageview-元素名稱區分大小寫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.