2011-05-20 33 views
2

設置標題欄我從http://labs.makemachine.net/2010/03/custom-android-window-title/安卓:在中間

創建使用style.I代碼一些自定義標題我還設置使用標題欄中的圖標setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.title_logo); 可它可以設置標題欄在中間?如果可能的話,我想使用自定義樣式進行設置。

編輯:我有校驗碼http://andmobidev.blogspot.com/2010/01/centering-title-of-window.html 它正常工作時,有標題欄沒有圖標,但設置時,我得到的錯誤java.lang.ClassCastException: android.widget.RelativeLayout 我不知道窗口如何繪製標題欄的圖標,我有檢查一個的博客 http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html

謝謝。

回答

5

我能找到真正的答案,最後我加載新的佈局中心的標題欄 組標題欄的代碼的OnCreate

final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

     setContentView(R.layout.main); 
     if (customTitleSupported) { 
      getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, 
        R.layout.title_bar); 
     } 
     final TextView myTitleText = (TextView) findViewById(R.id.title_bar); 
     if (myTitleText != null) { 
      myTitleText.setText(R.string.title_ble_melem); 

     } 

這裏是佈局

<?xml version="1.0" encoding="utf-8" ?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linear" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    > 
    <TextView 
     android:id="@+id/title_bar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="title bar" 
     android:layout_centerInParent="true" 
     android:layout_marginLeft="5dip" 
     android:textStyle="bold" 
     android:textColor="#ffffff" 
     android:textSize="20sp" 
     /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toLeftOf="@id/title_bar" 
     android:src="@drawable/ic_top_bar" 

    /> 
    </RelativeLayout > 

非常感謝您的回覆。

+0

+1發佈一個正確的答案,不依賴於無證結構。但是,如果您想添加到自定義標題欄的[進度條](http://stackoverflow.com/q/11254366/869501),您將無法使用。 – ateiob 2012-06-29 12:32:49

2

This page可以幫到你嗎?

此:

<TextView android:id="@android:id/title" 
    style="?android:attr/windowTitleStyle" 
    android:background="@null" 
    android:fadingEdge="horizontal" 
    android:gravity="center_vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 

和:

ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView(); 
LinearLayout root = (LinearLayout) decorView.getChildAt(0); 
FrameLayout titleContainer = (FrameLayout) root.getChildAt(0); 
TextView title = (TextView) titleContainer.getChildAt(0); 
title.setGravity(Gravity.CENTER); 

這應該在Activity.onCreate(...)設置內容視圖後調用。 來自源

+0

「點擊此處」在SO上不支持/建議這樣的回答。 – Nezam 2014-04-14 07:09:27

+0

謝謝!在我明白什麼是SO之前。 :) – 2014-04-14 19:06:37

+0

(y)投票up.as獎勵 – Nezam 2014-04-14 23:22:06

2

只是把代碼中的setContentView(R.layout.new)下...

((TextView)((FrameLayout)((LinearLayout)((ViewGroup) getWindow().getDecorView()).getChildAt(0)).getChildAt(0)).getChildAt(0)).setGravity(Gravity.CENTER); 

坦白地說這個代碼是從另一個計算器的問題複製:How to align center the title or label in activity?

我相信它可以幫助你交配!

;)

+0

感謝您的迴應,但我得到的錯誤引起:java.lang.ClassCastException:android.widget.RelativeLayout com.vvseksperten.HomeScreenActivity.onCreate(HomeScreenActivity.java:28)可能由於我的佈局,請檢查它在這裏http://pastebin.com/VffL6Mng – 2011-05-20 06:44:17

+0

嗨,我沒有錯誤,當只有textView,但當我在標題欄中設置圖標它給出錯誤 – 2011-05-20 08:23:28