2013-07-03 23 views
5

Tôi biết cách đặt phông chữ tùy chỉnh thành thanh hành động. Tôi chỉ cần mở rộng SherlockFragmentActivity và ghi đè lên setTitle như sau:Làm cách nào để đặt phông chữ tùy chỉnh thành thanh hành động theo ngữ cảnh của sherlock?

@Override 
public void setTitle(CharSequence title) { 
    String str = String.valueOf(title); 
    str = str.toUpperCase(Locale.getDefault()); 
    SpannableString s = new SpannableString(str); 
    MetricAffectingSpan span = new MetricAffectingSpan() { 
     @Override 
     public void updateMeasureState(TextPaint p) { 
      p.setTypeface(FontManager.INSTANCE.getAppFont()); 
     } 

     @Override 
     public void updateDrawState(TextPaint tp) { 
      tp.setTypeface(FontManager.INSTANCE.getAppFont()); 
     } 
    }; 

    s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    getSupportActionBar().setTitle(s); 
} 

Tuy nhiên, mọi thứ trở nên phức tạp với thanh tác vụ theo ngữ cảnh. Thư viện sử dụng một nhà máy để trả về thanh hành động theo ngữ cảnh, như sau:

ActionMode mode = getSherlockActivity().startActionMode(mActionModeCallback); 
mode.setTitle("whatever"); 

I CODD override ActionMode, nhưng lib sẽ không trả lại.

Bất kỳ ý tưởng nào?

Trả lời

4

tôi thấy là một chút phức tạp chút ...

Bạn sẽ cần phải tạo ra một View nơi bạn sẽ đặt TextView của bạn như tiêu đề, thiết lập font bạn muốn cho TextView này, và sử dụng setCustomView đặt của bạn xem bằng phông chữ mới.

Tôi hy vọng nó sẽ giúp bạn.

CẬP NHẬT

Bạn đã cố gắng tạo ra phương pháp riêng của mình, như thế này:

public void setActionModeTitle(CharSequence title) { 
    String str = String.valueOf(title); 
    str = str.toUpperCase(Locale.getDefault()); 
    SpannableString s = new SpannableString(str); 
    MetricAffectingSpan span = new MetricAffectingSpan() { 
     @Override 
     public void updateMeasureState(TextPaint p) { 
      p.setTypeface(FontManager.INSTANCE.getAppFont()); 
     } 

     @Override 
     public void updateDrawState(TextPaint tp) { 
      tp.setTypeface(FontManager.INSTANCE.getAppFont()); 
     } 
    }; 

    s.setSpan(span, 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

    actionMode.setTitle(s); 
} 
+0

là Có cách nào để thay đổi chỉ TextView sử dụng cho tiêu đề? –

+0

Bản cập nhật cho bạn. –

+0

Chỉnh sửa 2 đã hoạt động! tôi đã thử điều đó trước đó, vô ích. Nó khá đơn giản sau khi tất cả. cảm ơn! –

0
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    android:padding="@dimen/padding_very_small" 
    android:weightSum="2" > 

    <TextView 
     android:id="@+id/uRecomendTitleText" 
     style="@style/TitleStyle" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical" 
     android:layout_weight="0.5" 
     android:drawableLeft="@drawable/left_arrow" 
     android:text="@string/recommendation" 
     android:visibility="visible" /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1.5" 
     android:orientation="horizontal" > 

     <TextView 
      style="@style/TitleStyle" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|right" 
      android:gravity="center" 
      android:paddingRight="@dimen/padding_very_small" 
      android:text="@string/rank" /> 

     <TextView 
      android:id="@+id/uRecomendCount" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical|right" 
      android:background="@drawable/rounded_box" 
      android:gravity="center" /> 
    </LinearLayout> 

</LinearLayout> 
0
TextView txt=(TextView)findViewById(R.id.uRecomendTitleText); 
    Typeface AshtonsHand; 
     AshtonsHand = Typeface.createFromAsset(this.getAssets(), 
       IConstants.font); 
    txt.setTypeface(AshtonsHand); 
    getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 

    getSupportActionBar().setBackgroundDrawable(
      getResources().getDrawable(R.drawable.top_bg_back)); 

    // getSupportActionBar().setDisplayUseLogoEnabled(false); 
    // getSupportActionBar().setDisplayShowTitleEnabled(true); 
    // getSupportActionBar().setDisplayShowHomeEnabled(false); 
    // getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setCustomView(R.layout.friends_list_actionbar); 
Các vấn đề liên quan