按字母顺序排序按钮

时间:2012-06-19 10:05:54

标签: android sorting button

我在xml布局中创建了一些按钮。他们从3种不同语言的字符串资源中获取他们的名字。因此,按钮始终不按正确的字母顺序排列。所以我的问题就出现了。如何使按钮按所有3种语言按字母顺序排序?

此致 约翰

2 个答案:

答案 0 :(得分:1)

假设三个按钮位于LinearLayout中,您可以继承LinearLayout并覆盖getChildAt()方法以按照您想要的顺序返回视图(一旦加载了字符串,您就可以计算出来)。

E.g。

@Override
public View getChildAt(int index)
{
  // translate index into the ordering you want.
  // and put into newIndex...

  return super.getChildAt(newIndex);
}

答案 1 :(得分:0)

您可以为每种语言创建布局(请查看此document

另一个解决方案是在List中加载字符串,对它们进行排序,然后使用Button.setText将每个按钮的字符串从已排序的ListButton.setTag设置为为每个按钮提供此文本的唯一标记。之后,您可以使用标记相应地设置onClickListener

相关问题