2011-01-25 46 views

Trả lời

428

Để làm điều này trong layout.xml file:

android:textStyle 

Ví dụ:

android:textStyle="bold|italic" 

lập trình phương pháp này là:

setTypeface(Typeface tf) 

Thiết lập kiểu chữ và phong cách trong đó văn bản nên được hiển thị. Lưu ý rằng không phải tất cả các gia đình thực sự có các biến thể in nghiêng và in nghiêng, vì vậy bạn có thể cần phải sử dụng setTypeface(Typeface, int) để có giao diện mà bạn thực sự muốn.

310

Dưới đây là giải pháp

TextView questionValue = (TextView) findViewById(R.layout.TextView01); 
questionValue.setTypeface(null, Typeface.BOLD); 
21

Đặt thuộc tính

android:textStyle="bold" 
15

Nếu bạn đang vẽ nó thì điều này sẽ làm điều đó:

TextPaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG); 
14

Nó rất dễ dàng

setTypeface(Typeface.DEFAULT_BOLD); 
14

Đối với trường hợp bạn đang sử dụng phông chữ tùy chỉnh, nhưng không có kiểu chữ đậm cho font bạn có thể sử dụng:

myTextView.setText(Html.fromHtml("<b>" + myText + "</b>"); 
8

Xác định một phong cách mới với định dạng mà bạn muốn trong file style.xml trong thư mục các giá trị

<style name="TextViewStyle" parent="AppBaseTheme"> 
    <item name="android:textStyle">bold</item> 
    <item name="android:typeface">monospace</item> 
    <item name="android:textSize">16sp</item> 
    <item name="android:textColor">#5EADED</item> 

</style> 

sau đó, áp dụng phong cách này cho TextView bằng cách viết đoạn mã sau với các thuộc tính của TextView

style="@style/TextViewStyle" 
47

trong XML

android:textStyle="bold" //only bold 
android:textStyle="italic" //only italic 
android:textStyle="bold|italic" //bold & italic 

Bạn chỉ có thể sử dụng phông chữ cụ thể sans, serif & monospace qua xml, Java code có thể sử dụng phông chữ tùy chỉnh

android:typeface="monospace" // or sans or serif 

lập trình (mã Java)

TextView textView = (TextView) findViewById(R.id.TextView1); 

textView.setTypeface(Typeface.SANS_SERIF); //only font style 
textView.setTypeface(null,Typeface.BOLD); //only text style(only bold) 
textView.setTypeface(null,Typeface.BOLD_ITALIC); //only text style(bold & italic) 
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD); 
             //font style & text style(only bold) 
textView.setTypeface(Typeface.SANS_SERIF,Typeface.BOLD_ITALIC); 
             //font style & text style(bold & italic) 
13

Trong thế giới lý tưởng của bạn sẽ đặt thuộc tính kiểu văn bản trong định nghĩa XML bố cục của bạn như sau:

<TextView 
    android:id="@+id/TextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textStyle="bold"/> 

Có một cách đơn giản để đạt được kết quả tương tự động trong mã của bạn bằng cách sử dụng setTypeface phương pháp.Bạn cần phải vượt qua và đối tượng của Kiểu chữ lớp, sẽ mô tả kiểu phông chữ cho TextView đó. Vì vậy, để đạt được kết quả tương tự như với các định nghĩa XML ở trên, bạn có thể làm như sau:

TextView Tv = (TextView) findViewById(R.id.TextView); 
Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); 
Tv.setTypeface(boldTypeface); 

Dòng đầu tiên sẽ tạo ra dạng đối tượng phong cách được xác định trước (trong trường hợp này Typeface.BOLD, nhưng có nhiều hơn nữa được xác định trước). Một khi chúng ta có một thể hiện của kiểu chữ, chúng ta có thể đặt nó trên TextView. Và đó là nội dung của chúng tôi sẽ được hiển thị trên phong cách mà chúng tôi đã xác định.

Tôi hy vọng nó sẽ giúp bạn một lot.For thông tin tốt hơn bạn có thể truy cập

http://developer.android.com/reference/android/graphics/Typeface.html

3

Bạn có thể sử dụng cho các phông chữ

tạo một Class Name TypefaceTextView và mở rộng TextView

riêng tĩnh Bản đồ mTypefaces;

public TypefaceTextView(final Context context) { 
    this(context, null); 
} 

public TypefaceTextView(final Context context, final AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

public TypefaceTextView(final Context context, final AttributeSet attrs, final int defStyle) { 
    super(context, attrs, defStyle); 
    if (mTypefaces == null) { 
     mTypefaces = new HashMap<String, Typeface>(); 
    } 

    if (this.isInEditMode()) { 
     return; 
    } 

    final TypedArray array = context.obtainStyledAttributes(attrs, styleable.TypefaceTextView); 
    if (array != null) { 
     final String typefaceAssetPath = array.getString(
       R.styleable.TypefaceTextView_customTypeface); 

     if (typefaceAssetPath != null) { 
      Typeface typeface = null; 

      if (mTypefaces.containsKey(typefaceAssetPath)) { 
       typeface = mTypefaces.get(typefaceAssetPath); 
      } else { 
       AssetManager assets = context.getAssets(); 
       typeface = Typeface.createFromAsset(assets, typefaceAssetPath); 
       mTypefaces.put(typefaceAssetPath, typeface); 
      } 

      setTypeface(typeface); 
     } 
     array.recycle(); 
    } 
} 

dán phông chữ trong các phông chữ thư mục được tạo trong thư mục tài sản

<packagename.TypefaceTextView 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1.5" 
     android:gravity="center" 
     android:text="TRENDING TURFS" 
     android:textColor="#000" 
     android:textSize="20sp" 
     app:customTypeface="fonts/pompiere.ttf" />**here pompiere.ttf is the font name** 

Nơi các dòng trong cách bố trí cha mẹ trong xml

xmlns:app="http://schemas.android.com/apk/res/com.mediasters.wheresmyturf" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
52

Đơn giản chỉ cần bạn có thể làm như sau:

Đặt thuộc tính ở XML

android:textStyle="bold" 

Programatically phương pháp này là:

TextView Tv = (TextView) findViewById(R.id.TextView); 

Typeface boldTypeface = Typeface.defaultFromStyle(Typeface.BOLD); 

Tv.setTypeface(boldTypeface); 

Hy vọng điều này sẽ giúp bạn cảm ơn bạn.

+0

Hãy cho tôi biết kết quả – saeed

+0

@texas Cảm ơn bạn – saeed

3

Cách tốt nhất để đi là:

TextView tv = findViewById(R.id.textView); 
tv.setTypeface(Typeface.DEFAULT_BOLD); 
2

Giả sử bạn là một khởi động mới trên Android Studio, Đơn giản chỉ cần bạn có thể làm cho nó thực hiện theo quan điểm thiết kế XML bằng cách sử dụng

android:textStyle="bold"   //to make text bold 
android:textStyle="italic"  //to make text italic 
android:textStyle="bold|italic" //to make text bold & italic 
0
editText.setTypeface(Typeface.createFromAsset(getAssets(), ttfFilePath)); 
etitText.setTypeface(et.getTypeface(), Typeface.BOLD); 

sẽ đặt cả kiểu chữ cũng như kiểu thành chữ đậm.

+1

Bạn nên bao gồm giải thích ngắn gọn về câu trả lời của mình để mô tả tốt hơn trường hợp sử dụng và cách triển khai. –

+0

Điều này không quảng cáo bất kỳ điều gì mới đối với các câu trả lời hiện có. – nvoigt

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