標籤:android style class blog c code
方法一:使用開源項目ViewBadger,github上的地址:https://github.com/jgilfelt/android-viewbadger效果:
[java] view plaincopy
- <TextView
- android:id="@+id/tv1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="15dp"
- android:text="文本1" />
[java] view plaincopy
- <span style="font-family: Arial, Helvetica, sans-serif;"></span>
[java] view plaincopy
- tv = (TextView) findViewById(R.id.tv1);
- BadgeView badgeView = new BadgeView(MainActivity.this, tv); //執行個體化BadgeView
- badgeView.setText("12");
- // badgeView.setTextSize(8.5f); //設定文字的大小
- badgeView.setBadgePosition(BadgeView.POSITION_TOP_RIGHT);//設定在右上方
- badgeView.setTextColor(Color.DKGRAY); //字型的設定顏色
- badgeView.show(); //顯示
[java] view plaincopy
-
這樣就實現了上面的效果,注意引用開源項目ViewBadger時,要和建立的工程檔案在同一個檔案夾內,否則會出錯的 方法二:用架構架構布局FrameLayout效果:布局如下:這樣就可以了
[java] view plaincopy
- <FrameLayout
- android:id="@+id/frameLayout1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" >
-
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:padding="10dp"
- android:text="文本2" />
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="top|right"
- android:background="#FF0000"
- android:text="23"
- android:textColor="@android:color/white" />
-
- </FrameLayout>