我想設置樣式或字體在TextView
像圖像文字如下圖所示:如何設置樣式或字體爲TextView的文本在android中?
4
A
回答
11
您需要自定義字體,然後才能執行此操作:
Typeface mFont = Typeface.createFromAsset(getAssets(), "fonts/myFont.ttf");
MyTextView.setTypeface(mFont);
您必須在資產文件夾中創建「字體」文件夾。把你的字體放在那裏。
您當然也可以創建自定義TextView
。如果你喜歡,請參考this answer,我給了一段時間。
7
<TextView
style="@style/CodeFont"
android:text="@string/hello" />
你需要做的是codefont風格:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CodeFont" parent="@android:style/TextAppearance.Medium">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">monospace</item>
</style>
</resources>
直接從:http://developer.android.com/guide/topics/ui/themes.html
0
您可以創建一個layout.xml文件,其中包含您的textview。例如:
textView.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
style="@android:style/Holo.ButtonBar" >
如果你不想要這個,那麼你可以創建自定義樣式。事情是這樣的:
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Custom" parent="@android:style/TextAppearance.Large" >
<item name="android:typeface">monospace</item>
</style>
</resources>
,並在佈局文件中改變風格的東西,如:
style="@style/Custom"
3
還有另一種方式,如果你想改變它在許多TextViews,使用類:
public class MyTextView extends TextView {
public MyTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyTextView(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fonts/Ubuntu-L.ttf");
setTypeface(tf);
}
}
}
,並在佈局取代:
<TextView
...
/>
有了:
<com.WHERE_YOUR_CLASS_IS.MyTextView
...
/>
相關問題
- 1. 如何在android中的textview中設置雙文本樣式?
- 2. 如何爲文本視圖設置不同的字體樣式
- 3. 如何在JTextArea實例中爲所選文本設置粗體字體樣式
- 4. 如何在android中設置字體爲textview?
- 5. 如何在android中爲textview設置telugu字體?
- 6. 如何在android中爲TextView設置字體?
- 7. 如何在Android TextView中將字體樣式設置爲粗體,斜體和下劃線?
- 8. 如何設置在android中textview的樣式的填充?
- 9. 如何在textview中設置telugu字體?
- 10. 爲RTF文檔設置字體樣式
- 11. 如何在android studio的代碼中爲Textview文本設置粗體
- 12. 如何設置字體樣式在FPDF
- 13. 如何在Android中爲特定文本視圖設置字體?
- 14. 在textview中不設置文本粗體
- 15. 如何設置繪製文本的Arial字體樣式
- 16. 如何在Android中設置textView的字體?
- 17. 如何在Android中爲TextView設置字符數組作爲文本?
- 18. 如何設置WebView的字體樣式?
- 19. 在android中設置字體toa文本
- 20. 如何將蹦極字體樣式給android中的textview?
- 21. 如何在TextView文本中爲單詞設置自定義字體
- 22. 文本中的樣式(textView)
- 23. 如何在Android textview中設置字體寬度?
- 24. 設置包含文本的自定義視圖的樣式或字體,如默認的TextView?
- 25. Android - 將文本設置爲TextView
- 26. Android爲長文本設置TextView寬度
- 27. 安卓:設置文本樣式的TextView文本
- 28. 如何在textView中設置文字?
- 29. 如何在字段中設置佔位符文本的樣式?
- 30. 如何設置字體樣式?
由於其完全爲我工作。 – 2013-02-27 13:49:14