LabelView是在github上一个开源的标签库。其项目主页是:https://github.com/linger1216//labelview
LabelView为一个TextView,ImageView或者为ListView中适配器getView返回的View,增加一个左上角或者右上角的标签
这种需求设计在商城类APP、电商类APP中比较常用,这些APP展示的商品,通常会增加一些促销或者该类商品的特征。
LabelView集成自Android TextView,可以像使用Android TextView一样使用LabelView,LabelView使用简单,如代码所示:
布局代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://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/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#90CAF9"
android:gravity="center"
android:text="textView1"
android:textSize="30sp" /> <TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#9FA8DA"
android:gravity="center"
android:text="textView2"
android:textSize="30sp" /> <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/ic_launcher" /> <ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#B39DDB"
android:src="@drawable/ic_launcher" /> <View
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="100dip"
android:background="#e0e0e0" >
</View> </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); //为TextView1左上角添加一个标签
LabelView label1 = new LabelView(this);
label1.setText("Hot");
label1.setBackgroundColor(0xff03a9f4);
label1.setTargetView(findViewById(R.id.textView1), 4, Gravity.LEFT_TOP); //为TextView2右上角添加一个标签,点击标签移除
final LabelView label2 = new LabelView(this);
label2.setText("点击移除");
label2.setBackgroundColor(0xffE91E63);
label2.setTargetView(findViewById(R.id.textView2), 20,
Gravity.RIGHT_TOP);
findViewById(R.id.textView2).setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
label2.remove();
Toast.makeText(getApplicationContext(), "标签移除成功", 0).show();
}
}); //为ImageView1添加一个左上角标签,并且自定义标签字颜色
LabelView label3 = new LabelView(this);
label3.setText("推荐");
label3.setTextColor(Color.RED);
label3.setBackgroundColor(0xff03a9f4);
label3.setTargetView(findViewById(R.id.imageView1), 10,
Gravity.LEFT_TOP); //为IamgeView2添加一个右上角标签
LabelView label4 = new LabelView(this);
label4.setText("推荐");
label4.setBackgroundColor(0xffE91E63);
label4.setTargetView(findViewById(R.id.imageView2), 10,
Gravity.RIGHT_TOP); //为一个View添加一个左上角标签(ListView用)
LabelView label5 = new LabelView(this);
label5.setText("view");
label5.setTextColor(Color.BLUE);
label5.setBackgroundColor(0xffE91E63);
label5.setTargetView(findViewById(R.id.view), 10, Gravity.LEFT_TOP);
}
}