美文网首页
【Android】如何实现多个textview自动对齐最长的te

【Android】如何实现多个textview自动对齐最长的te

作者: 下雨天的小白鞋 | 来源:发表于2019-06-28 08:56 被阅读0次

    /**

    * 动态设置宽度,设置成最大宽度

    */

    private void calculateWidth() {

    //list集合装所有控件的宽

    List width_list =new ArrayList<>();

       //获取每个控件的宽

        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        tv_1.measure(spec, spec);

        int measuredWidthTicketNum = tv_1.getMeasuredWidth();

        width_list.add(measuredWidthTicketNum);

        spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        tv_2.measure(spec, spec);

        measuredWidthTicketNum = tv_2.getMeasuredWidth();

        width_list.add(measuredWidthTicketNum);

        spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

        tv_3.measure(spec, spec);

        measuredWidthTicketNum = tv_3.getMeasuredWidth();

        width_list.add(measuredWidthTicketNum);

        //获取最大宽

        int max = Collections.max(width_list);

    //设置宽度为最大宽度

        LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) tv_1.getLayoutParams();

        linearParams.width = max;

        tv_1.setLayoutParams(linearParams);

        linearParams = (LinearLayout.LayoutParams) tv_2.getLayoutParams();

        linearParams.width = max;

        tv_2.setLayoutParams(linearParams);

        linearParams = (LinearLayout.LayoutParams) tv_3.getLayoutParams();

        linearParams.width = max;

        tv_3.setLayoutParams(linearParams);

    }

        tvCreditName.setLayoutParams(linearParams);

    }

    相关文章

      网友评论

          本文标题:【Android】如何实现多个textview自动对齐最长的te

          本文链接:https://www.haomeiwen.com/subject/fpbjcctx.html