2013-05-29 107 views
0

我需要在ActionBar中有一個自定義菜單項,它只是顯示一些自定義的格式化文本。所以我想我會立即創建一個圖像並將其附加到菜單圖標上。所以在我的代碼,我想我可以使用佈局XML來編寫我的文字:ActionBarSherlock中的自定義菜單項

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="horizontal" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"> 
    <TextView android:textColor="#bcbcbb" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:text="Some Text"/> 
    <TextView android:textColor="#ffffff" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content" 
       android:id="@+id/replace_text" android:text="XXXX"/> 
</LinearLayout> 

然後創建一個臨時的佈局繪製到DrawableBitmap:

 LinearLayout layout = (LinearLayout)View.inflate(getApplicationContext(), R.layout.refund_text, null); 
     layout.setDrawingCacheEnabled(true); 
     layout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); 
     layout.layout(0, 0, mRefundLayout.getWidth(), layout.getHeight()); 
     layout.buildDrawingCache(true); 
     Bitmap bitmap = Bitmap.createBitmap(layout.getDrawingCache()); 
     BitmapDrawable bm = new BitmapDrawable(getBaseContext().getResources(), bitmap); 
     layout.setDrawingCacheEnabled(false); 
....do something with the bitmap 

但是該措施呼叫寬度和高度仍然是零。誰能告訴我我做錯了什麼?

問候 李

回答

0

如果你只是想控制在菜單項文本的顏色,你也許可以簡單地格式化逃脫(使用SpannableStringBuilder)中的CharSequence傳遞給MenuItem.add(INT groupId,int itemId,int order,CharSequence標題)。我還沒有嘗試過,讓我們知道它是否有效!

如果你最終想做更多的事情,那麼創建你自己的自定義的ActionProvider,在onCreateActionView()中膨脹你的佈局,並在onPerformDefaultAction()中「做東西」。