展会信息港展会大全

自定义AlertDialog 列表的显示 android开发学习
来源:互联网   发布日期:2015-10-03 11:31:46   浏览:1911次  

导读:请看下面的截图:弹出的对话框提供了几个列表供点击选择,但是每一项的后面多了一个居右显示的数字。首先采用如下方式构建对话框:String[] items = {所有分类,IT技术:11,Makingware:4,Makingware模板......

请看下面的截图:

弹出的对话框提供了几个列表供点击选择,但是每一项的后面多了一个居右显示的数字。

首先采用如下方式构建对话框:

String[] items = {"所有分类","IT技术:11","Makingware:4","Makingware模板:5","电子商务:7","麦金公司:16"};

AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);

builder.setIcon(android.R.drawable.ic_dialog_info);

builder.setTitle("文章分类");

ListAdapter catalogsAdapter = new ArrayAdapter<String>(Main.this, R.layout.catalogs_dialog, items);

builder.setAdapter(catalogsAdapter, new DialogInterface.OnClickListener(){

public void onClick(DialogInterface arg0, int arg1) {

//点击条目后的处理;

}

});

而其中的 R.layout.catalogs_dialog 对应的是 catalogs_dialog.xml :

<?xml version="1.0" encoding="utf-8"?>

<net.oschina.app.widget.CatalogTextView xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="40dip"

android:gravity="center_vertical"

android:paddingLeft="6dip"

android:paddingRight="6dip"

android:textSize="16dip"

android:textColor="#000"

/>

最后我们需要编写一个 CatalogTextView 类,继承 TextView:

package net.oschina.app.widget;

import android.content.Context;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Paint.Align;

import android.util.AttributeSet;

import android.widget.TextView;

/**

* 分类对话框条目显示

* @author Winter Lau

* @date 2012-1-10 下午12:56:59

*/

public class CatalogTextView extends TextView {

Paint pcPaint;

public CatalogTextView(Context context) {

this(context, null);

}

public CatalogTextView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public CatalogTextView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

pcPaint = new Paint();

pcPaint.setColor(Color.DKGRAY);

pcPaint.setTextSize(20);

pcPaint.setTypeface(this.getPaint().getTypeface());

pcPaint.setAntiAlias(true);

pcPaint.setTextAlign(Align.RIGHT);

}

@Override

protected void onDraw(Canvas canvas) {

String txt = this.getText().toString();

int idx = txt.lastIndexOf(':');

String catalog = null;

int pc = 0;

if(idx > 0){

catalog = txt.substring(0, idx);

try{

pc = Integer.parseInt(txt.substring(idx+1));

}catch(Exception e){

catalog = txt;

}

}

else

catalog = txt;

int left = this.getPaddingLeft();

int top = 38;

canvas.drawText(catalog, left, top, this.getPaint());//绘制分类名

//绘制文章数

if(pc > 0) {

String spc = String.format("%3d", pc);

if(pc > 99)

spc = "99+";

int pcLeft = (int)(this.getWidth() - 20);

canvas.drawText(spc, pcLeft, top, pcPaint);

}

canvas.restore();

}

}

完了,实现方法比较土,仅供参考。

赞助本站

人工智能实验室

相关热词: AlertDialog

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港