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));