自定义 线性布局_自定义系列:尺寸测量

针对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 实现效果

1487ba2bea579a3d4a3baf8c14a42b2a.gif

二、布局尺寸测量&宽高动态调整

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获取宽度
}

②实现效果

a07686f098f29590b61c300445cad581.png

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

③实现效果

a87747f5bb86d1a18c68d599cfae1dba.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值