TextView实现中间文字两侧图片的样式 和 左侧图片右侧文字

public class StandardView extends LinearLayout {
    private Context mContext;
    private TextView standard;
    private LinearLayout.LayoutParams lp;
    private Drawable standardLeftIcon, standardRightIcon;

    public StandardView(Context context) {
        super(context);
        mContext = context;
        setBackgroundResource(R.drawable.as_installed_bg);
        initView();
    }

    private void initView() {
        standard = new TextView(mContext);
        standard.setBackgroundColor(Color.WHITE);
        standard.setClickable(true);
        standard.setFocusable(true);
        standard.setText("蛋蛋扎拉 ");
        standard.setGravity(Gravity.CENTER);
        standard.setCompoundDrawablePadding(Util.Div(10));//左右图片距离文字的像素

        standardLeftIcon = getResources().getDrawable(R.drawable.mall_purchase_left);
        standardLeftIcon.setBounds(0, 0, standardLeftIcon.getIntrinsicWidth(), standardLeftIcon.getMinimumHeight()); //必须设置图片大小,否则不显示
        standardRightIcon = getResources().getDrawable(R.drawable.mall_purchase_right);
        standardRightIcon.setBounds(0, 0, standardRightIcon.getIntrinsicWidth(), standardRightIcon.getMinimumHeight());
        standardLeftIcon.setAlpha(255);
        standardRightIcon.setAlpha(255);
        standard.setCompoundDrawables(standardLeftIcon, null, standardRightIcon, null);

        standard.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                standard.setText(" 蛋蛋s扎拉");
            }
        });

        lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, Util.Div(40));
        lp.leftMargin = Util.Div(400);
        lp.topMargin = Util.Div(400);
        addView(standard, lp);

    }
}
   中间文字,两侧图片



   左侧图片(icon)右侧文字,图片距离文字像素20px:
  title.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.mall_purchase_left), null, null, null);
  title.setCompoundDrawablePadding(CoocaaApplication.Div(20));


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值