2016-02-08 28 views
5

Có thể sử dụng biểu tượng phông chữ trong bộ chọn thay vì có thể vẽ được không?Cách sử dụng biểu tượng phông chữ (phông chữ tuyệt vời) trong bộ chọn XML

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item android:drawable="@drawable/menu_home_press" android:state_pressed="true"></item> 
    <item android:drawable="@drawable/menu_home"></item> 
</selector> 

Trả lời

4

Tôi đã thay đổi màu văn bản trong bộ chọn thay vì có thể kéo được. Làm việc tốt.

Tạo lớp MyTextView kéo dài TextView

public class MyTextView extends TextView { 

    public MyTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(context); 
    } 

    public MyTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 

    public MyTextView(Context context) { 
     super(context); 
     init(context); 
    } 

    private void init(Context context) { 
     Typeface tf = Typeface.createFromAsset(context.getAssets(), 
       "fontawesome-webfont.ttf"); 
     setTypeface(tf); 
    } 
} 

Tạo text_color_selector.xml selector

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="#ff0000" android:state_pressed="true" /> 
    <!-- pressed --> 
    <item android:color="#ff0000" android:state_focused="true" /> 
    <!-- focused --> 
    <item android:color="#000000" /> 
    <!-- default --> 
</selector> 

Và sau đó sử dụng nó trong bạn bố trí

<com.example.mohsin.myapplication.MyTextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:textSize="50sp" 
     android:clickable="true" 
     android:textColor="@drawable/text_color_selector" 
     android:text="\uF242"> 

    </com.example.mohsin.myapplication.MyTextView> 
1

Bạn có thể sử dụng các biểu tượng font-tuyệt vời như sau:

1 - Sao chép font-tuyệt vời tập tin phông chữ để tài sản thư mục của bạn

2 - Tìm thấy các đối tượng ký tự cho các biểu tượng tôi muốn, sử dụng this trang

3 - Tạo mục nhập bằng strings.xml cho mỗi biểu tượng. Ví dụ:

<string name="icon_eg">&#xf13d;</string> 

4 - Tải phông chữ trong phương pháp onCreate và đặt nó cho Views thích hợp:

Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
... 
Button button = (Button)findViewById(R.id.like); 
button.setTypeface(font); 

Đừng quên để tham khảo các chuỗi theo quan điểm.

<Button 
    android:id="@+id/my_btn" 
    style="?android:attr/buttonStyleSmall" 
    ... 
    android:text="@string/icon_eg" /> 

kiểm tra this liên kết để biết thêm thông tin.

bạn không thể sử dụng làm công cụ chọn. nhưng bạn có thể tự động thay đổi các biểu tượng.

+0

Cảm ơn câu trả lời của bạn, nhưng tôi muốn sử dụng trong bộ chọn thay vì biểu tượng đơn giản –

+0

là không thể, vì đó là phông chữ (văn bản). – droidev

1

Cách đơn giản nhất!

Vì Thư viện hỗ trợ Android 26 và Android Studio 3.0, bạn có thể sử dụng hỗ trợ phông chữ gốc trong XML.

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

  1. Tạo thư mục phông chữ trong thư mục res
  2. Sao chép font chữ tập tin vào thư mục phông chữ
  3. Tạo tập tin nguồn Font mới gần file font

    <?xml version="1.0" encoding="utf-8"?> 
    <font-family xmlns:android="http://schemas.android.com/apk/res/android" 
        xmlns:app="http://schemas.android.com/apk/res-auto"> 
        <font 
         android:font="@font/font_awesome_font" 
         android:fontStyle="normal" 
         android:fontWeight="400" 
         app:font="@font/font_awesome_font" 
         app:fontStyle="normal" 
         app:fontWeight="400" /> 
    </font-family> 
    

    Sử dụng android:font cho API 26 và app:font cho API hỗ trợ từ 14.

  4. Bây giờ bạn có thể sử dụng fontFamily trong TextView:

    <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:fontFamily="@font/font_awesome" 
        android:text="\uF0da" /> 
    
  5. Để chèn font-tuyệt vời char nhập nó đang UTF với \u

LƯU Ý: Android Studio xem trước thiết kế doesn' t hiển thị font chữ-awesome char nhưng trong ứng dụng nó hoạt động chính xác.

Các vấn đề liên quan