android开发笔记(1-5)(易错点以及技术难点攻克)

本文详细介绍了如何解决ScrollView中嵌套ListView或GridView导致的焦点问题,并提供了清除webview缓存的方法、图片与文字组合显示的技巧、自定义带有文字的图片组件、viewpager在Activity中的应用以及子Activity生命周期的调用策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.scrollview中嵌套有listview或者gridview,从其他页面返回到这个页面,焦点总是跑到listview或者gridview上

解决办法:重写scrollview的下边方法

@Override
	protected int computeScrollDeltaToGetChildRectOnScreen(Rect rect) {
		return 0;
	}

2.webview清除缓存的方法:

webview.clearCache(true);
webview.clearHistory();

3.图片和文字的组合显示可以使用下边方法:

<TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="12dp"
            android:drawableLeft="@drawable/meal"
            android:drawablePadding="15dp"
            android:gravity="center_vertical"
            android:text="推荐"
            android:textColor="@color/default_left_content_color" />

4.图片底部写文字,可以用下边的办法:


/**
 * 带文字的图片
 */

public class TextImageView extends ImageView {

	protected Context mContext;
	private float textSize; // 文字大小
	private String text;

	private Paint paint;
	private TextPaint textPaint;
	private TextImageView textImageView;

	public TextImageView(Context context) {
		super(context);
		this.textImageView = this;
		init(context);
	}

	public TextImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.textImageView = this;
		init(context);
	}

	public TextImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.textImageView = this;
		init(context);
	}

	/**
	 * @param context
	 */
	private void init(Context context) {
		this.mContext = context;
		paint = new Paint();
		paint.setColor(Color.parseColor("#7f000000")); // 颜色
		paint.setStyle(Paint.Style.FILL);
		paint.setStrokeWidth(5);

		textPaint = new TextPaint();// 初始化画笔,为后面文字使用。
		textPaint.setAntiAlias(true);// 去除锯齿。
		try {
			textPaint.setTextSize(mContext.getResources().getDimension(
					R.dimen.size_28px)); // 设置文字大小
			
		} catch (Exception e) {
		}
		textPaint.setColor(Color.WHITE); // 颜色
		textPaint.setTextAlign(Paint.Align.CENTER);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		// 画边框
		Rect rec = canvas.getClipBounds();
		int top = rec.bottom;
		int left = rec.left;
		int right = rec.right;
		int bottom = rec.bottom;

		RectF rectf = new RectF(left, 0.8f * top, right, bottom);
		canvas.drawRect(rectf, paint);
		int textWidth = 0;
		try {
			textWidth = (int) textPaint.measureText(text);// 获取字体的宽度
		} catch (Exception e) {
		}
		String ellipsizeStr = "";
		int boottomtextWidth = getMeasuredWidth() - 15;
		if (textWidth > boottomtextWidth) {
			ellipsizeStr = (String) TextUtils.ellipsize(text, textPaint,
					boottomtextWidth, TextUtils.TruncateAt.END);
		} else {
			ellipsizeStr = text;
		}
		if (StringTools.isNotEmpty(ellipsizeStr)) {
			canvas.drawText(ellipsizeStr, 0.5f * right, 0.95f * top, textPaint);
		}

	}

	public float getTextSize() {
		return textSize;
	}

	public void setTextSize(float textSize) {
		this.textSize = textSize;
	}

	public String getText() {
		return text;
	}

	public void setTitle(String text) {
		this.text = text;
	}

	public void setImage(String picUrl) {
		AsynImageLoader.getInsatnce(mContext).showImageAsyn(this.textImageView,Format.null2Blank(picUrl));
	}

}

5.Activity中viewpager嵌套多个子activity,子activity生命周期的调用

       protected LocalActivityManager manager = null;
       ...
       manager = new LocalActivityManager(this, true);
       ...
       @Override
	public void onResume() {
		super.onResume();
		dealPreviousActivity(lastIndex + 1, false);
	}

	@Override
	protected void onStop() {
		dealPreviousActivity(1, true);
		super.onStop();
	}

	private void dealPreviousActivity(int index, boolean isOnStop) {
		if (manager != null) {
			Activity _activity = manager.getActivity("" + index);
			if (_activity == null)
				return;
			if (_activity instanceof HomeActivity) {
				if (isOnStop)
					((HomeActivity) _activity).goneOnScreen();
				else
					((HomeActivity) _activity).invisibleOnScreen();
			}
		}
	}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值