资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

AndroidListView高度问题-创新互联

有些页面中ListView只是整个页面的一小部分,需要上下滑动整个页面,ListView不让自己滑动,默认ListView只会显示第一个item。这个时候需要重新设置一下ListView的高度。如果ListView的item中有TextView并且TextView的行数大于1行,这个时候.重设ListView的高度却计算不出TextView的高度,会出现TextView只显示一行的情况。这个时候需要使用自定义的TextView,并且不要设置MaxLines这个属性。

创新互联建站-专业网站定制、快速模板网站建设、高性价比高县网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式高县网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖高县地区。费用合理售后完善,十多年实体公司更值得信赖。

设置ListView高度的代码:

public   static void SetHeigth(ListView list) {
        ListAdapter listAdapter = list.getAdapter();
        if (listAdapter == null) {
            return;
        }
        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            View listItem = listAdapter.getView(i, null, list);
            //             listItem.measure(LinearLayout.LayoutParams.MATCH_PARENT,0);
            listItem.measure(0,0);
            totalHeight += listItem.getMeasuredHeight();
        }
        ViewGroup.LayoutParams params = list.getLayoutParams();
        params.height = totalHeight+ (list.getDividerHeight() * (listAdapter.getCount() - 1));
        list.setLayoutParams(params);
    }

 自定义TextView的代码:

public class MyTextView extends TextView {
    private Context context;
    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        this.context=context;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Layout layout = getLayout();  
        if (layout != null) {  
            int height = (int)Math.ceil(getMaxLineHeight(this.getText().toString()))  
                    + getCompoundPaddingTop() + getCompoundPaddingBottom();  
            int width = getMeasuredWidth();              
            setMeasuredDimension(width, height);  
        }  
    }
    private float getMaxLineHeight(String str){  
        float height = 0.0f;  
        float screenW = context.getResources().getDisplayMetrics().widthPixels;  
        float paddingLeft = ((LinearLayout)this.getParent()).getPaddingLeft();  
        float paddingReft = ((LinearLayout)this.getParent()).getPaddingRight();  
        //这里具体this.getPaint()要注意使用,要看你的TextView在什么位置,这个是拿TextView父控件的Padding的,为了更准确的算出换行  
        int line = (int) Math.ceil( (this.getPaint().measureText(str)/(screenW-paddingLeft-paddingReft))); 
        height = (this.getPaint().getFontMetrics().descent-this.getPaint().getFontMetrics().ascent)*line;
        return height;
    }  
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文题目:AndroidListView高度问题-创新互联
浏览路径:http://cdkjz.cn/article/pchjh.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220