2012-11-12 53 views
2

我動態加載活動中的標題;每當標題太長時,我希望它能夠滾動,以便可以閱讀整個標題。滾動活動標題

我在一個自定義的XML文件,並試圖requestFeature,

android:singleLine="true" 
android:ellipsize="marquee" 
android:marqueeRepeatLimit ="marquee_forever" 
android:scrollHorizontally="true" 
android:focusable="true" 
android:focusableInTouchMode="true" 

我已經試過

TextView textView = (TextView) findViewById(android.R.id.title); 
textView.setSelected(true); 
textView.setEllipsize(TruncateAt.MARQUEE); 
textView.setMarqueeRepeatLimit(1); 

textView.setFocusable(true); 
textView.setFocusableInTouchMode(true); 
textView.requestFocus(); 
textView.setSingleLine(true); 

另一種方法是給了我在ellipsize nullpointers()。我真的很茫然。我怎樣才能達到這個效果?

回答

1

由於(TextView) findViewById(android.R.id.title)返回null,您的第二種方法將不起作用。

我建議按照Bhuro的answer,特別是關於如何自定義標題欄。基本上,你需要一個自定義的titlebar.xml來定義你想要的自定義標題欄(在你的情況下,只是一個TextView)。一個titlebar.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="fill_parent" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/myTitle" 
     android:text="This is my new title and it is very very long" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:singleLine="true" 
     android:ellipsize="marquee" 
     android:marqueeRepeatLimit ="marquee_forever" 
     android:scrollHorizontally="true" 
     android:focusable="true" 
     android:focusableInTouchMode="true" 
    /> 
</LinearLayout> 

然後你在活動指定:

final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

if (customTitleSupported) { 
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar); 
}