博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ScrollView子View为自定义View时需要注意的几点问题
阅读量:2189 次
发布时间:2019-05-02

本文共 2579 字,大约阅读时间需要 8 分钟。

         在内容现实不全时,通常的做法是在布局中加入ScrollView,使其可以滚定显示。

         在使用ScrollView时需要注意以下几点:

1. ScrollView要求其只有一个子View。当有多个View时,可以使用LinearLayout等布局包含,使其直接子View只有一个。

2. 当ScrollView的子View为自定义View时,

mScrollView = new ScrollView(this);   ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(              ViewGroup.LayoutParams.WRAP_CONTENT,            ViewGroup.LayoutParams.WRAP_CONTENT);   mScrollView.setLayoutParams(param);   mScrollView.addView((View) mMobileView);
  上边的代码中mMobileView 为自定义的View。此时子View的内容是不会显示的,需要通过以下两种方式的设置来显示子VIew

  方法一:mScrollView.setFillViewport(true); 本方法是使子View可以拉伸来填满整个屏幕

  方法二:在自定义View类中MobileView(class MobileView extends View)重写onMeasure方法

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {		// TODO Auto-generated method stub		setMeasuredDimension(GlobalFun.BWScreenWidth, GlobalFun.BWScreenHeight);//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);	}
调用setMeasuredDimension(GlobalFun.BWScreenWidth, GlobalFun.BWScreenHeight);来设置本View的宽和高,这样就会显示。注意宽度和高度必须大于设备的宽和高,此时才会滚动。

3.我遇到这样的情况,我自定义的View是全屏显示的,但是我又需要能够使当满足特定条件时,这个View能够指定其向上或者向下移动指定距离。这时我打算在再加一个子View实现占位的作用,从而实现宽和高为全屏的子View能够滚动。代码如下:

mScrollView = new ScrollView(this);      ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(      		ViewGroup.LayoutParams.WRAP_CONTENT,			ViewGroup.LayoutParams.WRAP_CONTENT);      mScrollView.setLayoutParams(param);      mFillView = new FillView(this);//占位的View//      mFillView.layout(l, t, r, b);                mLinearLayoutMobileMain = new LinearLayout(this);     LinearLayout.LayoutParams param3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);     mLinearLayoutMobileMain.setLayoutParams(param3);     mLinearLayoutMobileMain.setOrientation(LinearLayout.VERTICAL);             mLinearLayoutMobileMain.addView((View)mMobileView);     mLinearLayoutMobileMain.addView(mFillView);          mScrollView.addView(mLinearLayoutMobileMain);
由于ScrollView只能有一个子View,所以使用LinearLayout包含这两个子VIew(mFillView 和mMobileView)。

FillView中的代码

public class FillView extends View {	public FillView(Context context) {		super(context);		// TODO Auto-generated constructor stub	}	@Override	protected void onDraw(Canvas canvas) {		// TODO Auto-generated method stub		super.onDraw(canvas);	}	@Override	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {		// TODO Auto-generated method stub//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);		setMeasuredDimension(GlobalFun.BWScreenWidth, GlobalFun.BWScreenHeight);	}}
设置mFillVIew的 宽和高是屏幕的宽和高,同时自定义的mMobileView的宽和高也是屏幕的宽和高,这样就达到了目的。

转载地址:http://eyyub.baihongyu.com/

你可能感兴趣的文章
(PAT 1118) Birds in Forest (并查集)
查看>>
数据结构 拓扑排序
查看>>
(PAT 1040) Longest Symmetric String (DP-最长回文子串)
查看>>
(PAT 1145) Hashing - Average Search Time (哈希表冲突处理)
查看>>
(1129) Recommendation System 排序
查看>>
PAT1090 Highest Price in Supply Chain 树DFS
查看>>
(PAT 1096) Consecutive Factors (质因子分解)
查看>>
(PAT 1019) General Palindromic Number (进制转换)
查看>>
(PAT 1073) Scientific Notation (字符串模拟题)
查看>>
(PAT 1080) Graduate Admission (排序)
查看>>
Play on Words UVA - 10129 (欧拉路径)
查看>>
mininet+floodlight搭建sdn环境并创建简答topo
查看>>
【linux】nohup和&的作用
查看>>
Set、WeakSet、Map以及WeakMap结构基本知识点
查看>>
【NLP学习笔记】(一)Gensim基本使用方法
查看>>
【NLP学习笔记】(二)gensim使用之Topics and Transformations
查看>>
【深度学习】LSTM的架构及公式
查看>>
【python】re模块常用方法
查看>>
剑指offer 19.二叉树的镜像
查看>>
剑指offer 20.顺时针打印矩阵
查看>>