編輯:關於 Android 開發
一、TextView 的講解
《實例一》
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:background="#ccffee" android:padding="3sp" android:text="TextView的基本使用" android:textColor="#cd0000" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:layout_marginLeft="10dp" android:shadowColor="#F9F900" android:shadowDx="30.0" android:shadowDy="30.0" android:shadowRadius="3.0" android:text="帶陰影的TextView" android:textColor="#4A4AFF" android:textSize="30sp" /> <TextView android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginLeft="10dp" android:layout_marginTop="30dp" android:background="@drawable/txt_rectborder" android:padding="3sp" android:text="帶有邊框的TextView" android:textColor="#cd0000" /> <TextView android:id="@+id/text4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp" android:layout_marginLeft="10dp" android:layout_marginTop="30dp" android:background="@drawable/txt_radiuborder" android:padding="3sp" android:text="帶有圓角邊框的TextView" android:textColor="#cd0000" /> </LinearLayout>
《屬性講解》
基本使用:
id:為 TextView 設定一個組件 id,根據 id 可在 Java 程式碼中透過 findViewById() 取得該物件並設定屬性,使用 RelativeLayout 時參考組件也用 id。
layout_width:組件寬度,一般寫 wrap_content 或 match_parent(fill_parent),前者是內容多大控件就多大,後者會填滿父容器;也可設為特定大小。
layout_height:組件高度,同上。
gravity:設定控件中內容的對齊方向。
text:設定顯示的文字內容,建議寫到 string.xml 再透過 @String/xxx 引用,不建議直接寫在 "" 裡。
textColor:設定字體顏色,建議透過 colors.xml 引用。
textStyle:設定字體風格,三個可選值:normal(無效果)、bold(加粗)、italic(斜體)。
textSize:字體大小,單位一般用 sp。
background:控件背景顏色,可以是圖片。
帶有陰影的 TextView:
android:shadowColor:設定陰影顏色,需與 shadowRadius 一起使用。
android:shadowRadius:設定陰影的模糊程度,設為 0.1 就變成字體顏色了,建議使用 3.0。
android:shadowDx:設定陰影在水平方向的偏移。
android:shadowDy:設定陰影在垂直方向的偏移。
帶有邊框的 TextView:
(1)沒有圓角:在 drawable 目錄下定義 shape 類型的 xml 檔,並在 TextView 的 background 屬性中使用:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <!--設置一個黑色寬度為5px的一個邊框--> <stroke android:color="#000000" android:width="5px"/> <!--漸變--> <gradient android:angle="270" android:endColor="#C0C0C0" android:startColor="#FCD209"/> <!-- 設置一一下邊距--> <padding android:top="5px" android:bottom="5px" android:left="5px" android:right="5px"/> </shape>
(2)有圓角:同樣在 drawable 目錄下定義 shape 類型的 xml 檔:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="https://schemas.android.com/apk/res/android"> <!--設置一個黑色寬度為5px的一個邊框--> <stroke android:color="#000000" android:width="5px"/> <!-- 設置邊距--> <padding android:top="5dp" android:bottom="5dp" android:left="5dp" android:right="5dp"/> <!-- 設置一個透明背景色--> <solid android:color="#87CEEB"/> <!-- 設置圓角--> <corners android:bottomLeftRadius="50px" android:bottomRightRadius="50px" android:topLeftRadius="50px" android:topRightRadius="50px"/> </shape>
實例二:帶有圖片的 TextView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/text1" android:layout_width="200dp" android:layout_height="200dp" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="Android" android:textColor="#cd0000" android:drawableTop="@mipmap/ic_launcher" android:drawableBottom="@mipmap/ic_launcher" android:drawableLeft="@mipmap/ic_launcher" android:drawableRight="@mipmap/ic_launcher" /> <LinearLayout android:layout_width="match_parent" android:layout_height="100dp" android:layout_marginTop="50dp" android:orientation="horizontal"> <TextView android:id="@+id/text2" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="Android1" android:textColor="#cd0000" android:drawableTop="@mipmap/ic_launcher" /> <TextView android:id="@+id/text3" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="Android2" android:textColor="#cd0000" android:drawableTop="@mipmap/ic_launcher" /> <TextView android:id="@+id/text4" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="Android3" android:textColor="#cd0000" android:drawableTop="@mipmap/ic_launcher" /> <TextView android:id="@+id/text5" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="Android4" android:textColor="#cd0000" android:drawableTop="@mipmap/ic_launcher" /> </LinearLayout> </LinearLayout>
屬性講解:drawableTop(上)、drawableButtom(下)、drawableLeft(左)、drawableRight(右),另外可用 drawablePadding 設定圖片與文字間距。由於 XML 中無法設定圖片大小,需在 Java 中實現:
import android.app.Activity;import android.graphics.drawable.Drawable; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.text1); // 數組下標是0~3,依次是左上右下 Drawable[] drawables = textView1.getCompoundDrawables(); // 設置上面圖片的大小 drawables[1].setBounds(200,0,200,200); // 給TextView是四個方向設置圖片也是左上右下,如果某個方向上面沒有圖片設置為null即可 textView1.setCompoundDrawables(drawables[0],drawables[1],drawables[2],drawables[3]); } }
當文字中出現 URL、E-Mail、電話號碼、地圖時,可透過設定 autoLink 屬性;點擊對應文字即可跳轉至預設 APP,例如點擊號碼跳轉至撥號界面。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="50dp" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="百度https://baidu.com" android:autoLink="web" android:textColor="#cd0000" /> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginTop="10dp" android:gravity="center" android:textSize="20sp" android:background="#ccffee" android:padding="3sp" android:text="123456" android:autoLink="phone" android:textColor="#cd0000" /> </LinearLayout>
說明:all 表示全部包含、自動識別協議頭。在 Java 中可呼叫 setAutoLinkMask(Linkify.ALL),此時可不寫協議頭由 autolink 自動識別,但還要為該 TextView 設定 setMovementMethod(LinkMovementMethod.getInstance()),否則點擊沒有效果。
實例四:TextView 對 HTML 的支持。並非所有 html 標籤都支持,常用如下:
<ul> <li><font>:設置顏色和字體。</li> <li><big>:設置字體大號</li> <li><small>:設置字體小號</li> <li><i><b>:斜體粗體</li> <li><a>:連接網址</li> <li><img>:圖片</li> </ul>
package com.example.test3; //文字和鏈接 import android.app.Activity; import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod;import android.text.util.Linkify; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.text1); // 定義html字符串 String s1 = "<font color='blue'><b>百度一下,你就知道~:</b></font><br>"; s1 += "<a href = 'https://www.baidu.com'>百度</a>"; // 設置給TextView,需要調用Html.fromHtml()方法 textView1.setText(Html.fromHtml(s1)); // 設置鏈接的類型,如果是all可以不寫協議頭,會自動識別 textView1.setAutoLinkMask(Linkify.ALL); // 在Java中調用鏈接,需要設置下面的方法,不然沒有作用 textView1.setMovementMethod(LinkMovementMethod.getInstance()); } }
package com.example.test3; //圖片 import android.app.Activity; import android.graphics.drawable.Drawable;import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod; import android.text.util.Linkify; import android.widget.TextView; import java.lang.reflect.Field; public class MainActivity extends Activity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.text1); String s1 = "圖片:<img src = 'ic_launcher'/><br>"; textView1.setText(Html.fromHtml(s1, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { Drawable draw = null; try { // 得到圖片的位置 Field field = R.mipmap.class.getField(source); // 得到圖片的id int resourceId = Integer.parseInt(field.get(null).toString()); // 得到圖片 draw = getResources().getDrawable(resourceId); // 設置圖片的大小 draw.setBounds(0, 0, draw.getIntrinsicWidth(), draw.getIntrinsicHeight()); } catch (Exception e) { e.printStackTrace(); } return draw; } }, null)); } }
實例四:SpannableString & SpannableStringBuilder 定制文本
(1)SpannableString、SpannableStringBuilder 基本上與 String 差不多,也是用來存儲字串,但它們的特殊之處在於有一個 setSpan() 函數,能為這些字串添加各種格式(Span),將原本的字串以不同樣式顯示,例如加下劃線、加背景色、改變字體顏色、用圖片替換指定文字等。總之,它們首先也是傳字串,但可對字串添加額外的樣式資訊,而 String 則不行。
(2)注意:這些額外資訊需被所用的方式支持,例如將 SpannableString 傳給 TextView;也有不支持的情況,此時 SpannableString 和 SpannableStringBuilder 會退化為 String,直接顯示原始字串。
(3)兩者區別:SpannableString 像 String,構造時傳入一個 String,之後無法更改內容也無法拼接多個;而 SpannableStringBuilder 更像 StringBuilder,可透過 append() 拼接多個 String。
(4)void setSpan (Object what, int start, int end, int flags):為特定範圍的字串設定 Span 樣式,可設定多個。flags 參數標識了在標記範圍前後緊貼插入新字元時是否套用同樣樣式。
參數說明:object what 為對應的各種 Span;int start 為開始位置(索引從 0 開始);int end 為結束位置(特效不含此位置);int flags 有四個取值:SPAN_EXCLUSIVE_EXCLUSIVE(前後都不包括)、SPAN_EXCLUSIVE_INCLUSIVE(前不包括後包括)、SPAN_INCLUSIVE_EXCLUSIVE(前包括後不包括)、SPAN_INCLUSIVE_INCLUSIVE(前後都包括)。
(5)對應的各種 Object:
<ul> <li><BackgroundColorSpan> 背景色</li> <li><ClickableSpan> 文本可點擊,有點擊事件</li> <li><ForegroundColorSpan> 文本顏色(前景色)</li> <li><MaskFilterSpan> 修飾效果,如模糊(BlurMaskFilter)、浮雕(EmbossMaskFilter)</li> <li><MetricAffectingSpan> 父類,一般不用</li> <li><RasterizerSpan> 光柵效果</li> <li><StrikethroughSpan> 刪除線(中劃線)</li> <li><SuggestionSpan> 相當於占位符</li> <li><UnderlineSpan> 下劃線</li> <li><AbsoluteSizeSpan> 絕對大小(文本字體)</li> <li><DynamicDrawableSpan> 設置圖片,基於文本基線或底部對齊。</li> <li><ImageSpan> 圖片</li> <li><RelativeSizeSpan> 相對大小(文本字體)</li> <li><ReplacementSpan> 父類,一般不用</li> <li><ScaleXSpan> 基於x軸縮放</li> <li><StyleSpan> 字體樣式:粗體、斜體等</li> <li><SubscriptSpan> 下標(數學公式會用到)</li> <li><SuperscriptSpan> 上標(數學公式會用到)</li> <li><TextAppearanceSpan> 文本外貌(包括字體、大小、樣式和顏色)</li> <li><TypefaceSpan> 文本字體</li> <li><URLSpan> 文本超鏈接</li> </ul>
import android.app.Activity; import android.graphics.Color; import android.graphics.Typeface; import android.graphics.drawable.Drawable;import android.os.Bundle; import android.text.Spannable; import android.text.SpannableString; import android.text.Spanned; import android.text.style.ForegroundColorSpan; import android.text.style.ImageSpan; import android.text.style.StrikethroughSpan; import android.text.style.StyleSpan; import android.text.style.URLSpan; import android.text.style.UnderlineSpan; import android.widget.TextView; public class MainActivity extends Activity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.text1); SpannableString span = new SpannableString("紅色打電話斜體刪除線綠色下劃線圖片:."); //1.設置背景色,setSpan時需要指定的flag,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE(前後都不包括) span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //2.用超鏈接標記文本 span.setSpan(new URLSpan("tel:123456"), 2, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); //3.用樣式標記文本(斜體) span.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), 5, 7, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //4.用刪除線標記文本 span.setSpan(new StrikethroughSpan(), 7, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //5.用下劃線標記文本 span.setSpan(new UnderlineSpan(), 10, 16, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //6.用顏色標記 span.setSpan(new ForegroundColorSpan(Color.GREEN), 10, 13,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); //7.獲取Drawable資源 Drawable d = getResources().getDrawable(R.mipmap.ic_launcher); d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); //8.創建ImageSpan,然後用ImageSpan來替換文本 ImageSpan imgspan = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); span.setSpan(imgspan, 18, 19, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); textView1.setText(span); } }
package com.example.test3; import android.app.Activity; import android.graphics.Color; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.text.SpannableString; import android.text.SpannableStringBuilder; import android.text.Spanned; import android.text.TextPaint; import android.text.method.LinkMovementMethod; import android.text.style.ClickableSpan; import android.text.style.ImageSpan; import android.view.View; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private TextView textView1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView1 = (TextView) findViewById(R.id.text1); StringBuffer sb = new StringBuffer(); for(int i = 0;i < 20;i++){ sb.append("好友" + i + ","); } String user = sb.substring(0,sb.lastIndexOf(",")).toString(); textView1.setMovementMethod(LinkMovementMethod.getInstance()); textView1.setText(addClickPart(user), TextView.BufferType.SPANNABLE); } private SpannableStringBuilder addClickPart(String str) { // 定義點贊的圖標 ImageSpan imgspan = new ImageSpan(MainActivity.this,R.mipmap.ic_launcher); imgspan.getDrawable().setBounds(0,0,imgspan.getDrawable().getIntrinsicWidth(),imgspan.getDrawable().getIntrinsicHeight()); SpannableString ss = new SpannableString("p."); ss.setSpan(imgspan,0,1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE); // 連接多個字符串 SpannableStringBuilder ssb = new SpannableStringBuilder(ss); ssb.append(str); String[] users = str.split(","); if(users.length > 0){ for(int i = 0;i < users.length;i++){ final String name = users[i]; final int start =str.indexOf(name) + ss.length(); ssb.setSpan(new ClickableSpan() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show(); } @Override public void updateDrawState(TextPaint ds) { super.updateDrawState(ds); //刪除下劃線,設置字體顏色為藍色 ds.setColor(Color.BLUE); ds.setUnderlineText(false); } },start,start + name.length(),0); } } return ssb.append("等" + users.length + "個人覺得很贊"); } }
實例五:跑馬燈
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:id="@+id/txtOne" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit="marquee_forever" android:focusable="true" android:focusableInTouchMode="true" android:text="你整天說著日了狗日了狗,但是你卻沒有來,呵呵呵呵呵呵呵呵呵呵~"/> </LinearLayout>
實例六:行間距、字間距、自動換行(預設自動換行)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <!-- 自動換行--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:singleLine="false" android:text="呵呵呵呵呵呵呵呵呵呵~~~~~~~~~~aaaaaa" android:maxLines="5"/> <!-- 字符間距--> <TextView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:textScaleX="2.0" android:text="呵呵呵呵呵呵呵呵呵呵~~~~~~~~~~aaaaaa"/> <!-- 行間距lineSpacingExtra設置大小、lineSpacingMultiplier設置行的倍數默認是1。0--> <TextView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:lineSpacingExtra="10dp" android:text="呵呵呵呵呵呵呵呵呵呵~~~~~~~~~~aaaaaa"/> <TextView android:layout_marginTop="20dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="30sp" android:lineSpacingMultiplier="1.5" android:text="呵呵呵呵呵呵呵呵呵呵~~~~~~~~~~aaaaaa"/> </LinearLayout>
Nagios 分組出圖的程式碼實現(下):讓同組主機的服務圖一次畫完
承接上篇:從「選得中」到「畫得出」上一節我們完成了分組全選:沿用 Nagios 自帶的主機/服務分組,在列表裡補上複選框,讓同一組的機器可以一次勾選,並加上服務分類的下拉
手機安全衛士實作:應用程式管理與使用者/系統程式分類
軟體管理頁是手機安全衛士中很實用的模組,除了列出已安裝應用,還能區分使用者程式與系統程式,並提供分享、執行、卸載與詳情等操作。 取得應用資訊 AppInfos.getA
為什麼 WinForm 的自適應這麼難?一次思路轉換到 WPF
問題:靠計算撐不住 WinForm 要做到表單與控制項的自適應,一直都不是容易的事。不是完全做不到,而是如果只靠「計算縮放因子、再逐一手動調整座標與尺寸」這條路,永遠解決
Android 學習指南之二十九:WebView 元件完整用法解析
在行動裝置上開瀏覽器看網頁,是最典型的行動上網行為。如果我們想在自己的 App 裡直接呈現網頁,不必跳出去開外部瀏覽器,Android 提供了 WebView 這個專用元