1.自定义listview类
package com.example.administrator.standardOA.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.ListView;
import com.r93535.standardOA.R;
/**
* Created by Duqianlong on 2018/11/26.
*/
public class ConstraintHeightListView extends ListView {
private float mMaxHeight = 100;//默认100px
public ConstraintHeightListView(Context context) {
this(context, null);
}
public ConstraintHeightListView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ConstraintHeightListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ConstraintHeightListView, 0, defStyleAttr);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ConstraintHeightListView, 0, defStyleAttr);
int count = array.getIndexCount();
for (int i = 0; i < count; i++) {
int type = array.getIndex(i);
if (type == R.styleable.ConstraintHeightListView_maxHeight) {
//获得布局中限制的最大高度
mMaxHeight = array.getDimension(type, -1);
}
}
array.recycle();
}
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//获取lv本身高度
int specSize = MeasureSpec.getSize(heightMeasureSpec);
//限制高度小于lv高度,设置为限制高度
if (mMaxHeight <= specSize && mMaxHeight > -1) {
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Float.valueOf(mMaxHeight).intValue(),
MeasureSpec.AT_MOST);
}
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
2.style添加
<declare-styleable name="ConstraintHeightListView">
<attr name="maxHeight" format="dimension"/>
</declare-styleable>
3.使用方式
<com.example.administrator.standardOA.view.ConstraintHeightListView
android:id="@+id/commList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/dp_10"
app:maxHeight="@dimen/dp_450" />
注意:根布局需要有
xmlns:app="http://schemas.android.com/apk/res-auto"