自定义一个Toast(技巧)

自定义一个Toast

要实现这样的效果:

 

 

使用下面的代码:

  LayoutInflater inflater = LayoutInflater.from(this);
  View view = inflater.inflate(R.layout.book_reading_seekbar_toast, null);
  TextView chapterNameTV = (TextView) view.findViewById(R.id.chapterName);
  TextView percentageTV = (TextView) view.findViewById(R.id.percentage);
  chapterNameTV.setText(chapterName);
  percentageTV.setText(df.format(persent * 100) + "%");
  
  Toast toast = new Toast(this);
  toast.setGravity(Gravity.BOTTOM, 0, PixelFormat.formatDipToPx(this, 70));
  toast.setDuration(Toast.LENGTH_LONG);
  toast.setView(view);
  toast.show();

就可以实现这样的效果,但是还不够!得借助于布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@null" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="24dp"
        android:layout_marginRight="24dp"
        android:background="@drawable/book_reading_toast_bg"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingBottom="37dp"
        android:paddingTop="39dp" >

        <TextView
            android:id="@+id/chapterName"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="center"
            android:singleLine="true"
            android:text="第八百张  鲤鱼跳龙门"
            android:textColor="#ffffff"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/percentage"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/chapterName"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="8dp"
            android:gravity="center"
            android:text="45%"
            android:textColor="#ffffff"
            android:textSize="16sp" />
    </RelativeLayout>

</LinearLayout>
借助于LInearLayout,把布局撑开,然后借助于里面的RelativeLayout 调整布局参数!打到最终效果!


但是这样还存在一个问题:虽然效果可以一样,但是有时候有些东西在代码中设置比较方便!所以要使用以上代码中的:

PixelFormat.formatDipToPx(this, 70)

这个的作用是:将美工的切图中的70dp,转换成pix 的值。 这样就可以在不同的屏幕显示相同的效果,可以达到适配所有屏幕!
我把这个转换的方法也发上来吧:

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;

/**
 * 像素转换类

 */
public class PixelFormat {
 /**
  * 把dip单位转成px单位
  *
  * @param context
  *            context对象
  * @param dip
  *            dip数值
  * @return
  */
 public static int formatDipToPx(Context context, int dip) {
  DisplayMetrics dm = new DisplayMetrics();
  ((Activity) context).getWindowManager().getDefaultDisplay()
    .getMetrics(dm);
  return (int) Math.ceil(dip * dm.density);
 }

 /**
  * 把px单位转成dip单位
  *
  * @param context
  *            context对象
  * @param px
  *            px数值
  * @return
  */
 public static int formatPxToDip(Context context, int px) {
  DisplayMetrics dm = new DisplayMetrics();
  ((Activity) context).getWindowManager().getDefaultDisplay()
    .getMetrics(dm);
  return (int) Math.ceil(((px * 160) / dm.densityDpi));
 } www.2cto.com

}
刚才忘记添加那个圆角了,呵呵,现在加上:
book_reading_toast_bg .xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >

    <solid android:color="#aa000000" />

    <corners android:radius="10dp" />

    <padding
        android:bottom="5dp"
        android:left="5dp"
        android:right="5dp"
        android:top="5dp" />

</shape>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值