针对wrap_content属性,对其进行测量。
一、文本&图形尺寸
1.1 文本宽度使用Paint的measureText方法进行测量:
// 获取指定文本的宽度(其实就是长度)
public static float getTextWidth(String text, float textSize) {
if (TextUtils.isEmpty(text)) {
return 0;
}
Paint paint = new Paint(); // 创建一个画笔对象
paint.setTextSize(textSize); // 设置画笔的文本大小
return paint.measureText(text); // 利用画笔丈量指定文本的宽度
}
1.2 文本长度使用FontMetrics类的相关属性:
// 获取指定文本的高度
public static float getTextHeight(String text, float textSize) {
Paint paint = new Paint(); // 创建一个画笔对象
paint.setTextSize(textSize); // 设置画笔的文本大小
Paint.FontMetrics fm = paint.getFontMetrics(); // 获取画笔默认字体的度量衡
return fm.descent - fm.ascent; // 返回文本自身的高度
//return fm.bottom - fm.top + fm.leading; // 返回文本所在行的行高
}
1.3 图形尺寸
图形使用iv_measure.getDrawable().getIntrinsicHeight()与iv_measure.getDrawable().getIntrinsicWidth()测量
public void initImgShow(){
tv_showimg.setText("图像高度:"+iv_measure.getDrawable().getIntrinsicHeight()+"n图像宽度为:"+iv_measure.getDrawable().getIntrinsicWidth());
}
1.4 实现效果
二、布局尺寸测量&宽高动态调整
2.1 测量线性布局的高度
①线性布局高度获取
public float getLLHeight(View v){
LinearLayout ll = (LinearLayout)v;
//获得线性布局的布局参数
ViewGroup.LayoutParams params = ll.getLayoutParams();
if(params == null){
params = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
}
//获得布局参数中的宽高规格
int widthSpec = ViewGroup.getChildMeasureSpec(0,0,params.width);
int heightSpec;
if(params.height>0){
heightSpec = View.MeasureSpec.makeMeasureSpec(params.height, View.MeasureSpec.EXACTLY);
}else {
heightSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
}
//重新测量线性布局的宽高
ll.measure(widthSpec,heightSpec);
return ll.getMeasuredHeight();//返回测量后的高度值,使用getMeasuredWidth获取宽度
}
②实现效果
2.2 宽高动态调整
在ScrollView中添加ListView,会导致ListView只显示一项,通过自定义的ListView,重写onMeasure,可解决该问题。
①自定义的myNotScrollListView
public class myNotScrollListView extends ListView {
public myNotScrollListView(Context context){
super(context);
}
public myNotScrollListView(Context context, AttributeSet attrs){
super(context,attrs);
}
public myNotScrollListView(Context context,AttributeSet attrs,int def){
super(context,attrs,def);
}
//重写onMeasure,用于自定义视图高度
public void onMeasure(int widthMeasureSpec,int heightMeasureSpec){
int exSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec,exSpec);
}
}
②页面代码:与前文《列表与BaseAdapter适配器》所用的Spinner方式一样。
public void initPeriBefore(){
PeriAdapter periAdapter = new PeriAdapter(this,myperiList);
lv_peri_before.setAdapter(periAdapter);
lv_peri_before.setOnItemLongClickListener(new myPeriListener());
lv_peri_before.setOnItemClickListener(new myPeriListener());
}
public void initPeriAfter(){
PeriAdapter periAdapter = new PeriAdapter(this,myperiList);
nslv_peri_after.setAdapter(periAdapter);
nslv_peri_after.setOnItemLongClickListener(new myPeriListener());
nslv_peri_after.setOnItemClickListener(new myPeriListener());
}
class myPeriListener implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
//监听点击
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
Toast.makeText(ScrollAndListview.this,String.format("第%d张是《%s》",position+1,myperiList.get(position).name),Toast.LENGTH_SHORT).show();
}
//监听长按
public boolean onItemLongClick(AdapterView<?> parent, View view,int position,long id){
Toast.makeText(ScrollAndListview.this,String.format("即将删除第%d张专辑:%s",position+1,myperiList.get(position).name),Toast.LENGTH_SHORT).show();
return true;
}
}
③实现效果