根据显示的标题长度动态定位ActionBar Spinner的右上角箭头

时间:2012-08-08 23:32:17

标签: android android-actionbar android-ui android-spinner

在Google+应用中,ActionBar Spinner的右上角箭头的位置会适应当前显示的字符串的长度。例如,选择 Family 时,Spinner的长度似乎更短,而选择 Acquaintances 时则更短。

根据标题的长度,我需要做什么才能让右箭头动态定位?我的猜测是我可以在 styles.xml 中设置一个属性来为我做这个。

这是我的styles.xml:

<style name="MyStyle" parent="android:style/Theme.Holo">
    <item name="android:actionDropDownStyle">@style/MyDropDownNav</item>
</style>

<style name="MyDropDownNav" parent="android:style/Widget.Holo.Spinner">
    <item name="android:AttributeHere">AttributeValue</item>
</style>

这是我在MainActivity.java中的代码:

ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

// Specify a SpinnerAdapter to populate the dropdown list.
SpinnerAdapter dataAdapter = new ArrayAdapter<String>(
        actionBar.getThemedContext(),
        android.R.layout.simple_list_item_1, android.R.id.text1,
        new String[] { "AAAAAAAAAAAAAAA", "BB" });

// Set up the dropdown list navigation in the action bar.
actionBar.setListNavigationCallbacks(dataAdapter, this);

与Google+不同,我的右箭头的位置始终是固定的。因此,如果我从下拉列表中选择“AAAAAAAAAAAAAAA”到“BB”,则箭头根本不会移动。

1 个答案:

答案 0 :(得分:0)

请参考android pure Calendar的代码,在其适配器中,getView()始终返回Calendar中的当前项,以便Spinner的宽度等于当前项目。实际上,Spinner中的ActionBar宽度始终计算为其最长项目的长度。 Calendar在这里做了一些特殊的处理。