Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

電商 App 常見的「Hot」角標:開源 LabelView 元件實戰

編輯:關於 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>

Java 程式碼

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 決定角標掛在哪個角。
  • 在 ListView 的 adapter 中使用時,記得在 getView() 裡為每次重用的 convertView 重新設定標籤,否則會出現位置錯亂。
  • 角標是獨立 View,覆蓋在目標之上,因此父容器的 clipChildren 若為 false 才能完整顯示超出邊界的部分。

整體來說,LabelView 把「電商行角標」這個高頻需求抽象成三行程式碼,是個很值得借鏡的元件設計範例。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved