『壹』 android中TextView在界面顯示的時候,我想在鍵盤顯示的上方加兩個按鈕,一個是確定,一個是取消.
那個不是TextView 是EditText ,你只要把EditText放在Button上面就行了 代碼如下:
粘進去運行就行了 activity隨便寫個就行 記得配manifest~~~
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<EditText android:id="@+id/et" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_above="@id/add" />
<Button android:text="ok" android:id="@+id/add"
android:layout_alignParentBottom="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button android:text="cancle" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@id/add" />
</RelativeLayout>
『貳』 android 如何在textview最後一個字後邊追加一個ImageView圖標
textview的配置項里有個屬性,android:drawableEnd,設了就可以。你可以去看下官方文檔,有好幾個屬性。
『叄』 如何在Android中為TextView動態設置drawableLeft等
只需要代碼設置setCompoundDrawables (Drawable left, Drawable top, Drawable right, Drawable bottom); 這個方法即可,如果說drawableLeft為null,即沒有對應的圖標
『肆』 如何在 Android 應用中使用 FontAwesome 圖標
FontAwesome 可以節省許多時間,原因如下:
首先,你不需要擔心不同手機上的屏幕解析度問題。如果你使用png圖片,你需要在包裡面對每個圖標至少包含四種不同的版本。不僅如此,在某些超清屏幕上,你的圖標可能會出現顆粒感。這顯然是要避免的。但如果使用FontAwesome,你只需包含一個"TTF」文件。
其次,你可以依賴於當今最豐富的免費圖標集之一。而且因為其在web上被廣泛的使用,現在用戶已經習慣了FontAwesome的風格。你不必再浪費時間去尋找漂亮的豐富的可以免費商用的圖標集合,我並不是說不存在這樣的圖標集,因為確實存在,但是非常稀少。
1. FontAwesome 的工作原理
我們先花點時間來了解一下FontAwesome 的工作原理。FontAwesome 圖標集背後的思想非常簡單,圖標被視為字元(character)。你可以能已經注意到一些奇怪的字元被作為文本對待,你可以輕易的拷貝 β 字元或者 ∑ 字元。你甚至可以在普通的文本編輯框中這樣做。還可以改變它們的大小和顏色。這是因為瀏覽器 - 以及文本編輯框 - 把這些字元視為文本。
FontAwesome 通過包含廣泛的圖標擴展了這一概念。你可以把它比喻成用圖標指定的不能打出的Unicode字元。
FontAwesome
看一眼 FontAwesome's cheatsheet 就知道我在說什麼了。你選擇列表中的一個圖標,記下它的Unicode的字元,在TextView中使用它告訴安卓使用FontAwesome字體來渲染。
2. 導入字體文件
讓我們來看一個例子。下載和導入FontAwesome 的TrueType 文件到項目。你可以從 GitHub上下載FontAwesome 的assets。
當你下載了FontAwesome之後,你會發現裡麵包含了一些文件和文件夾。大部分都是對web項目有用的。我們只對位於fonts目錄的 fontawesome-webfont.ttf感興趣。
在你的安卓項目中,導航到 app > src > main。 main 目錄應該包含了一個叫 assets的文件夾。如果沒有就創建一個。在assets 文件夾中創建另一個fonts文件夾,並把fontawesome-webfont.ttf 添加到這個文件夾。
主義 fonts 文件夾並不是必須的。你可以直接把FontAwesome 的字體文件放在 assets 目錄,但是把相同類型的文件放在專門的目錄裡面比較方便。只要FontAwesome 字體在assets 或者子目錄之下就行。
3. 創建一個幫助類
現在你已經成功的把FontAwesome 字體文件包含在了自己的安卓項目里,是時候使用它了。我們會創建一個幫助類來讓事情變得簡單點。這個類要使用到android.graphics.Typeface。Typeface類指定typeface 以及一個字體的特徵。它用於指明text在繪制(以及測量)的時候該如何顯示。
創建一個新的名叫FontManager的java類:
1
2
3
4
5
6
7
8
9
10
public class FontManager {
public static final String ROOT = "fonts/",
FONTAWESOME = ROOT + "fontawesome-webfont.ttf";
public static Typeface getTypeface(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
}
}
如果你想在項目中使用其他的字體,把字體放在helper 類裡面就可以了。類似於:
1
yourTextView.setTypeface(FontManager.getTypeface(FontManager.YOURFONT));
我們需要做的就這么多,但是我們可以做的更好。使用上面的方法的話,我們需要為每個想當成圖標來使用的TextView創建一個變數。但作為一個程序員,我們都很懶,對吧?
圖標一般都是包含在一個ViewGroup,比如一個RelativeLayout或者LinearLayout中。我們可以寫一個方法,爬遍指定xml parent 並且遞歸的覆蓋每個TextView的字體。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class FontManager {
// ...
public static void markAsIconContainer(View v, Typeface typeface) {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
markAsIconContainer(child);
}
} else if (v instanceof TextView) {
((TextView) v).setTypeface(typeface);
}
}
}
假設你的布局文件是這樣的:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/icons_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
要把這三個TextView標記為圖標,我們重寫onCreate方法,並添加如下代碼片段:
1
2
Typeface iconFont = FontManager.getTypeface(getApplicationContext(), FontManager.FONTAWESOME);
FontManager.markAsIconContainer(findViewById(R.id.icons_container), iconFont);
4. 使用你想要的圖標
現在輪到有意思的部分了。訪問 FontAwesome的GitHub頁面 並瀏覽所給的圖標。選擇三個你喜歡的。我准備選擇三個chart圖標,分別是 area chart icon, pie chart icon, 以及 line chart icon。
在你的項目中,進入 values 文件夾並創建一個新的文件:icons.xml。這個文件將被作為字典使用,它將把Unicode 字元和相應的圖標用可讀的名字匹配起來。這意味著我們需要為每個圖標創建一個入口。
1
2
3
4
5
<resources>
<string name="fa_icon_areachart"></string>
<string name="fa_icon_piechart"></string>
<string name="fa_icon_linechart"></string>
</resources>
你可以在FontAwesome cheatsheet或者圖標的 詳情頁面 找到你感興趣圖標的代碼。
下一步就是在布局的TextView裡面引用這些字元串。這是最終的樣子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/fa_icon_areachart" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/fa_icon_piechart" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="@string/fa_icon_linechart" />
如果你打開Android Studio的布局編輯器,你會看到它無法渲染這些圖標。這是不正常的。編譯並啟動應用,你又會發現圖標是正常渲染了的。
看起啦很小是吧?改變圖標的大小很簡單,你只需改變textSize屬性就是了。改變圖標的顏色也一樣簡單,編輯textColor屬性就是了。
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:textSize="45sp"
android:textColor="#9b59b6"
android:text="@string/fa_icon_areachart" />
『伍』 android中如何在TextView中顯示文字的同時加上笑臉一樣的圖標
使用 SpannableString 這個類,可以防止圖片和添加文本,然後放置在textView中就可以了
『陸』 Android 的Textview控制項
對android不是很熟悉。
我的理解是:這個TextView是在XML裡面告訴android這是一個textview控制項。
然後android:text是告訴android這個textview上面顯示的內容的。這個textview就只有這么一個屬性,因此只能設置一次。如果是在代碼裡面修改的話,應該是隨便你怎麼改都沒有問題。
『柒』 android自定義view怎麼畫textview
自定義view繪制的時候,都是通過畫筆/畫布,使用一些畫線/畫圓的,通過設置畫筆的顏色/字體/文字大小,畫布的背景等等來繪制想要的圖形.如果需要監聽點擊事件,也可以添加ontouch事件.