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

/**

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

*/

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自动对齐最长的textview)