RadioButton中心可绘制/布局展平

时间:2016-04-12 10:36:38

标签: android android-layout android-relativelayout android-radiobutton

我必须对我的布局做一些展平,然后在它的下面。 TextViewRadioButton百分比位于左侧的相同位置,并使用动画进行切换。第二个TextView显示了一些符合宽度的文字。问题是RadioButton短于TextView%,但是我使用alignLeftalignRight获得相同的宽度,即使重力设置为{{1} radio radioable左对齐。我找到了this topic,但在材质之前有点老了,目前这个类比较复杂。任何建议如何在没有自己的宽度(或文本视图的百分比宽度)中居此收音机!添加另一个center? (precent和radio可能在另一个ViewGroup并设置RelativeLayout,就像我在尝试展平前一样)

centerInParent

1 个答案:

答案 0 :(得分:0)

好的,我的案例有一些解决方法。我不接受这篇文章,因为它不是一个正确的答案。我还在指望另一种方式,更优雅;)

从xml以上我设置了固定宽度百分比并删除了ems,几乎没有区别。还删除了左右对齐无线电属性中的百分比,因此它在两个维度(wrap_content)中都有自己的大小。下一步是代码:

if(radioMarginLeft<0) {
    int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(
                    percentTextView.getLayoutParams().width, View.MeasureSpec.AT_MOST);
    int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);

    radioButton.measure(widthMeasureSpec, heightMeasureSpec);

    radioMarginLeft = (percentTextView.getLayoutParams().width -
                    radioButton.getMeasuredWidth())/2;
}

((RelativeLayout.LayoutParams) radioButton.getLayoutParams()).leftMargin=radioMarginLeft;

测量无线电,百分比现在具有固定宽度,因此无需测量。 radioMarginLeft仅计算一次并保留在方法之外,在我的情况下,它可能甚至是静态的。

附加:因为Add margin between a RadioButton and its label in Android?(请查看最受欢迎的答案),请小心设置广播的填充