网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

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高度问题-创新互联
标题URL:http://bjjierui.cn/article/pchjh.html

其他资讯