編輯:關於 Android 程式設計
先看成果,是不是跟你預期的一樣?

電商類 App 的商品圖上,幾乎都會掛一個「Hot」、「新品」、「限時」之類的角標。自己用 Canvas 畫一個三角形再加文字不算難,但要處理左上/右上定位、文字測度、移除動畫,還是直接用現成元件划算。
LabelView 就是這樣一個開源標籤庫,專案首頁:https://github.com/linger1216/labelview
它能為任意 TextView、ImageView,或 ListView 中 adapter 的 getView() 所回傳的 View,加上一個左上角或右上角的角標。由於它繼承自 Android 的 TextView,用法幾乎跟你熟悉的 TextView 一模一樣。
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.zzw.textlabelview.MainActivity">
<TextView
android:id="@+id/tv_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#CCAAFF"
android:gravity="center"
android:text="textView"
android:textSize="16sp" />
<TextView
android:id="@+id/tv_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFAADD"
android:gravity="center"
android:text="textView"
android:textSize="16sp" />
<ImageView
android:id="@+id/iv_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" />
<ImageView
android:id="@+id/iv_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#BBDDDB"
android:src="@drawable/ic_launcher" />
<View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="10dip"
android:background="#eeeeee" />
</LinearLayout>
package com.zzw.textlabelview;
import com.lid.lib.LabelView;
import com.lid.lib.LabelView.Gravity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 為 TextView 加上左上角標籤
LabelView label1 = new LabelView(this);
label1.setText("Hot");
label1.setBackgroundColor(0xffffaf00);
label1.setTargetView(findViewById(R.id.tv_one), 10, Gravity.LEFT_TOP);
// 為 TextView 加上右上角標籤,點擊可移除
final LabelView label2 = new LabelView(this);
label2.setText("點擊移除");
label2.setBackgroundColor(0xffffee00);
label2.setTargetView(findViewById(R.id.tv_two), 10, Gravity.RIGHT_TOP);
findViewById(R.id.tv_two).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
label2.remove();
Toast.makeText(getApplicationContext(), "標籤移除成功", Toast.LENGTH_SHORT).show();
}
});
// 為 ImageView 加上左上角標籤,並自訂字色
LabelView label3 = new LabelView(this);
label3.setText("推薦");
label3.setTextColor(Color.RED);
label3.setBackgroundColor(0xffffaf00);
label3.setTargetView(findViewById(R.id.iv_one), 10, Gravity.LEFT_TOP);
// 為 ImageView 加上右上角標籤
LabelView label4 = new LabelView(this);
label4.setText("推薦");
label4.setBackgroundColor(0xffffee00);
label4.setTargetView(findViewById(R.id.iv_two), 10, Gravity.RIGHT_TOP);
// 為任意 View 加上左上角標籤(ListView 情境同理)
LabelView label5 = new LabelView(this);
label5.setText("view");
label5.setTextColor(Color.BLUE);
label5.setBackgroundColor(0xffffee00);
label5.setTargetView(findViewById(R.id.view), 10, Gravity.LEFT_TOP);
}
}
setTargetView(view, distance, gravity) 的第二個參數是角標與目標 View 的間距,單位為 dp。Gravity.LEFT_TOP 與 Gravity.RIGHT_TOP 決定角標掛在哪個角。getView() 裡為每次重用的 convertView 重新設定標籤,否則會出現位置錯亂。clipChildren 若為 false 才能完整顯示超出邊界的部分。整體來說,LabelView 把「電商行角標」這個高頻需求抽象成三行程式碼,是個很值得借鏡的元件設計範例。
用frame動畫打造 Android 蝸牛風格進度條
好的介面細節能顯著提升使用者的第一印象。本文實作一個以蝸牛爬行動畫作為「滑塊」的進度條,讓等待過程更有趣味性。 一、準備frame動畫資源 <?xml versi
微信未讀消息截圖怎麼製作?趣味聊天畫面生成教學
你是否看過朋友分享那種滿屏未讀訊息的微信聊天截圖?其實這種趣味畫面是可以自己製作的。透過線上小工具,你能自訂頭像、暱稱與訊息內容,讓聊天介面顯示大量未讀訊息,截圖分享給朋
在 Visual Studio 中打造 wxWidgets C++ GUI 開發環境(含中文亂碼解法)
wxWidgets 是跨平台 C++ GUI 函式庫中相當經典的一個。本文示範的建置方式為 Unicode + DLL + Debug,主要考量是最後產出的 exe 不會
用 ListView 打造圓角表格樣式的清單效果
ListView 除了單調的列表,也能透過客製 Adapter 與背景資源,做出具有視覺層次的表格樣式。本文示範一個只有三筆資料的清單,並依位置套用不同的圓角背景(頂端、