2012-12-19 45 views
2

我會用圖標和文本自定義應用程序的每個活動的標題欄。我嘗試了一些方法,但它們不起作用。有誰能夠幫助我?如何自定義標題欄與Android應用程序中的圖像?

這是我的代碼示例:

public class TitleBar extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 
    setContentView(R.layout.activity_main); 

    if (customTitleSupported) { 
     getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); 
    } 
    // user can also set color using 「Color」 and then 「Color value constant」 
    // myTitleText.setBackgroundColor(Color.GREEN); 
    } 
} 

在XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
    android:id="@+id/myTitleText" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="appuntiiiiiii" /> 

</LinearLayout>` 
+0

可能重複[自定義標題與圖像](http://stackoverflow.com/questions/2086989/custom-title-with-image) – nbrooks

回答

1

我知道這可能是矯枉過正滿足您的需求,但如果你真的想要的東西冷卻你應該嘗試實現動作條。它是API11的官方授權,但您可以實施ActionBarSherlock,外觀和工作原理幾乎相同。我喜歡這個!

至於你的代碼,沒有ImageView,所以你怎麼可能顯示圖像。你需要一個imageView之前你的TextView

相關問題