我想在android中使用RelativeLayout和RadioGroup制作一个标签栏,该标签栏有一个指示灯,它将滑动以指示RadioGroup中的活动RadioButton。RelativeLayout:将视图对齐同级视图组的孩子
这些是定制的单选按钮,这些按钮实际上是带有文本图标的矩形,所有内容居中,它使用自定义状态选择器作为背景。
这里是我当前的层次:
<RelativeLayout> // Main view of the tab bar
<RadioGroup> // The buttons
<RadioButton />
<RadioButton />
<RadioButton />
</RadioGroup>
<ImageView /> // The indicator
</RelativeLayout>
的想法我是被点击一个单选按钮的时候,我会对准指标顶端&底部选择单选按钮(和动画吧)。但是,似乎layout_align<Edge>
只适用于兄弟元素,而不适用于其他视图组的成员,即我可以对齐到RadioGroup本身,但不适用于其中的RadioButton。
我想将指标作为RadioGroup的成员,但由于RadioGroup是LinearLayout的扩展,似乎没有办法将它放在给定RadioButton的边缘。
任何人都可以想到我可以如何解决这个问题?或者,也许有比我的animate-align-to-button技术更好的解决方案?
UPDATE 在@superjos的帮助下,我设法解决了这个问题。
我不得不给每个按钮一个已知的高度,而不是使用wrap_content
(不理想,但对于这个项目,它应该可以正常工作)。使指标高度与按钮的高度相匹配。确保RadioGroup设置为包装内容并在父视图中垂直居中。将指标设置为alignTop
和toRightOf
RadioGroup。然后,点击该按钮听众:
int prevY, newY;
int prevButtonIndex, newButtonIndex;
public void moveIndicator(button, indicator, index) {
prevY = newY;
newY = prevY + (index - prevButtonIndex) * button.getHeight();
TranslateAnimation animation = new TranslateAnimation(0, 0, prevY, newY);
animation.setDuration(500);
animation.setFillAfter(true); // stay at final animation position
indicator.setAnimation();
animation.start();
}
太棒了!实施看起来也不错! – superjos