zoukankan      html  css  js  c++  java
  • android带有文字的图片按钮的两种实现方式

    android带有文字的图片按钮的两种实现方式

    1). TextView对Button用相对布局,这要要求按钮的背景图片要留下空白位置给文字。这种方式开发比较简单,适合做一些风格一致的Button。

    <RelativeLayout android:id="@+id/relative"

    android:layout_width="wrap_content" android:layout_height=" wrap_content "

    android:gravity="center">

     

    <Button android:id="@+id/button"

    android:layout_width="fill_parent" android:layout_height="fill_parent" 

    android:background="@drawable/button_bg"/>

     

        <TextView android:layout_height="wrap_content"

       android:layout_width="fill_parent" 

    android:text=" 图片上的文字"

    android:layout_alignParentBottom="true" android:gravity="center" />

    </RelativeLayout>

     

    2).自定义控件继承Button, 重写onDraw(Canvas canvas)把图片绘制上去,字体位置可以改变,不依赖已有的图片。这种方式比较灵活,可以实现复杂的需求。

    public class CustomButton  extends Button

    {

    Public CustomButton (Context context , AttributeSet  attrs)

    {

    super.( context, attrs);

    bitmap = BitmapFactory.decodeResource(getResources(), resourceId); 

    }

     

    Protected void onDraw(Canvas canvas)

    {

    //图片顶部居中显示  

     int x=(this.getMeasuredWidth()-bitmap.getWidth())>>1; 

    int y=0;

    canvas.drawBitmap(bitmap,x,y,null);

    //让文字在底部显示  

    canvas.translate(0, (this.getMeasuredHeight()>>1)-(int)this.getTextSize());

    super.onDraw(canvas);

    }

     

  • 相关阅读:
    codeforces round #433 div2
    bzoj1951
    bzoj3620
    bzoj2286
    bzoj1513
    bzoj4390
    codeforces round 430 div 2
    bzoj3339
    准备实现体积蒙皮
    看牛顿法的改进与验证局部收敛
  • 原文地址:https://www.cnblogs.com/wangmars/p/3261952.html
Copyright © 2011-2022 走看看