android launcher主菜单显示图片和文字之BubbleTextView

来源:互联网 发布:2017淘宝卖什么比较好 编辑:程序博客网 时间:2024/06/02 23:03

在android平台中,Launcher在显示所有应用程序的图标和文字时,会用到一个类BubbleTextView,这个类继承TextView;

我们只要看过手机的都知道,主菜单图标由两部分组成:一个是应用的图标,另一部分是应用程序的名称;

下面请看我写的一个关于bubbletextView的例子:

1.新建一个工程:

 

2.新建一个colors.xml文件,定义一些颜色:

<resources><color name="white">#0000FF</color><color name="black">#000000</color><color name="bubble_dark_background">#B2ffb6c1</color></resources>


 

3.新建一个BubbleTextView类,继承TextView:

package com.lovehly.applicationdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.RectF;import android.text.Layout;import android.util.AttributeSet;import android.widget.TextView;public class BubbleTextView extends TextView {private static final int PADDING_H = 5;private static final int PADDING_V = 1;private static final int CORNER_RADIUS = 8;private Paint mPaint;private RectF mRect = new RectF();public BubbleTextView(Context context) {super(context);init();}public BubbleTextView(Context context, AttributeSet attrs) {super(context, attrs);init();}public BubbleTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}private void init() {setFocusable(true);setPadding(PADDING_H, 0, PADDING_H, PADDING_V);mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);mPaint.setColor(getContext().getResources().getColor(R.color.bubble_dark_background));}@Overrideprotected void drawableStateChanged() {invalidate();super.drawableStateChanged();}@Overridepublic void draw(Canvas canvas) {// TODO Auto-generated method stubsuper.draw(canvas);final Layout layout = getLayout();final RectF rect = mRect;final int left = getCompoundPaddingLeft();final int top = getExtendedPaddingTop();rect.set(left + layout.getLineLeft(0) - PADDING_H,top + layout.getLineTop(0) - PADDING_V, Math.min(left + layout.getLineRight(0) + PADDING_H, getScrollX()+ getRight() - getLeft()),top + layout.getLineBottom(0) + PADDING_V);canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, mPaint);}}

 

4.再修改一下main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableLeft="@drawable/portal_ring_outer_holo"        android:textColor="@color/black"        android:text="@string/hello_world" />        <com.lovehly.applicationdemo.BubbleTextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:drawableTop="@drawable/portal_ring_outer_holo"        android:textColor="@color/white"        android:text="护士love西子"        /></LinearLayout>


5.再修改androidmanifest.xml文件,其实有一点大家可能不知道:launcher其实就是透明的activity,所以我们要把activity设置成透明的样式,添加属性:

android:theme="@android:style/Theme.Wallpaper.NoTitleBar"  即可。


 

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.lovehly.applicationdemo"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="17" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name="com.lovehly.applicationdemo.MainActivity"            android:theme="@android:style/Theme.Wallpaper.NoTitleBar"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>


运行结果:

 

 

注意这是两种不同的主菜单样式:图片在文字的左边和图片在文字的上面;这只需要设置一个属性即可:

android:drawableLeft   图片在文字的左边

android:drawableTop 图片在文字的上面

android:drawableRight 图片在文字的右面

android:drawableBottom 图片在文字的下方。

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 笔记本电脑打不开光盘怎么办 电脑dvd弹不出来怎么办 安卓手机浏览器有个锁退不了怎么办 华硕电脑com端口没有怎么办 淘宝子账号认证过于频繁怎么办 现在手机淘宝卖家怎么办 注册新的淘宝账户怎么办 淘宝账号被限制买东西了怎么办 淘宝账号被冻结了怎么办 淘宝买的手机坏了怎么办 淘宝账户被限制登录怎么办 美团退款申诉失败怎么办 微信申诉不回来怎么办 特岗登录名忘记了怎么办 天猫账号被限制怎么办 微信双开被限制登录怎么办 云服务显出账号已过期怎么办 全国对讲机显示账号过期怎么办 淘宝账号身份证过期了怎么办 清理垃圾软件打打不开怎么办 电脑清理后软件打不开了怎么办 手机黑屏开不了机怎么办 红米手机wifi打不开怎么办 苹果8开不开机怎么办 mac电脑打不开机怎么办 steam改密码上限了怎么办 qq加密了登不了怎么办 别人登我的淘宝怎么办 植物2被禁止登录怎么办 淘宝网东西未收到怎么办 淘宝网卖家不许退货退款怎么办 身份证以前开过淘宝店怎么办 支付宝登录名尚未激活怎么办 淘宝退货卖家不收货退款买家怎么办 淘宝账号刷得太多违规怎么办 闲鱼交易关闭了怎么办 淘宝店开了没做怎么办 微店店铺严重违规怎么办 淘宝违规扣2分怎么办 淘宝被扣6分怎么办 淘宝被扣2分怎么办