Android View中的setMeasuredDimension方法
来源:互联网 发布:淘宝装修软件哪个好 编辑:程序博客网 时间:2024/06/07 12:55
继承View,实现自己想要的组件,那么需要使用到setMeasuredDimension这个方法,这个方法决定了当前View的大小,请看代码:
View的代码:
- package cc.mdev.test;
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.text.TextPaint;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.view.View;
- public class MyScrollView extends View {
- public MyScrollView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- public MyScrollView(Context context) {
- super(context);
- }
- @Override
- protected void onDraw(Canvas canvas) {
- TextPaint paint = new TextPaint();
- paint.setAntiAlias(true);
- canvas.drawColor(Color.GRAY);
- for (int i = 10; i < 500; i++) {
- canvas.drawText("This is the scroll text.", 10, i, paint);
- i = i+15;
- }
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- String tag="onMeasure";
- Log.e(tag, "Scroll View on measure...");
- setMeasuredDimension(200, 800);
- }
- @Override
- protected void onScrollChanged(int l, int t, int oldl, int oldt) {
- String tag = "onScrollChanged";
- Log.e(tag, "Scroll....");
- super.onScrollChanged(l, t, oldl, oldt);
- }
- }
布局文件:
- <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#fff" > <Button android:text="Button01" android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> <ScrollView android:layout_width="fill_parent" android:layout_height="fill_parent"> <cc.mdev.test.MyScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"/> </ScrollView> </LinearLayout>
效果就是自定义视图的大小为
200, 800,并且放入到ScrollView中,ScrollView会启作用,如果不使用setMeasuredDimension这个方法,那么
ScrollView不会有作用。
0 1
- Android View中的setMeasuredDimension方法
- Android View中的setMeasuredDimension方法
- Android View中的setMeasuredDimension方法
- View中的setMeasuredDimension方法
- View.onMeasured的默认实现 (onMeasure必须调setMeasuredDimension)
- android自定义控件(四) View中的方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- android自定义控件(四) View中的方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- android自定义控件(四) View中的方法
- Android view中的requestLayout和invalidate方法
- Android view中的requestLayout和invalidate方法
- 2015-04-21 纳尼?!和上一篇已经差了一个月的时间?!
- Too many arguments to function call,expected 0,have *
- oracle trunc 简单使用
- 绿色家居新革命 变频技术让你聪明节电
- toast中的context
- Android View中的setMeasuredDimension方法
- eCos中的线程与同步
- Linux修改IP和DNS
- 破除“系统学习”的情结
- FIR.im Weekly - 一些散落各处的移动开发好资源
- java Unsafe.java(二)
- 2013 腾讯一面面试题
- JQuery插件最佳实践
- web 系统权限控制