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 图片在文字的下方。
- android launcher主菜单显示图片和文字之BubbleTextView
- android 如何定制Launcher主菜单中应用程序图标在主菜单中的显示顺序
- android 如何定制Launcher主菜单中应用程序图标在主菜单中的显示顺序
- android 如何让应用程序在主菜单[launcher]程序列表中动态显示或隐藏
- 如何定制Launcher主菜单中应用程序的显示顺序
- Android之动画主菜单
- 源码修改之修改Launcher隐藏主菜单图标
- android 如何定制Google launcher的主菜单图标
- android如何定制Google launcher的主菜单图标
- redmine 主菜单显示
- Android之经典Launcher主菜单模块学习
- Android之经典Launcher主菜单模块学习
- android 仿iphone主题之主菜单
- android 仿iphone主题之主菜单
- android开发之TextView显示加载的文字和图片
- 如何客制化Launcher的主菜单图标?
- 如何定制Launcher主菜单中应用程序图标的显示顺序?
- Android 自定义主菜单
- asmack 搜索好友信息
- Sum It Up poj 1711
- VC++界面美化
- 电脑快捷键使用
- 因子分析(Factor Analysis)
- android launcher主菜单显示图片和文字之BubbleTextView
- 显示一个QLable
- jvm初探
- Android开发-将一个项目作为lib导入到另外一个项目中
- 无法打开项目文件.csproj”
- 去除字符串中重復的值函數
- 使用eclipse开发ndk之:导入现有的ndk工程(NDK例子-Hello-jni导入到eclipse)
- linux驱动程序安装2种方式(模块方式 和 直接编译进内核)
- 简述企业租用服务器的管理与维护