作为的ToggleButtons ActionBar的自定义视图行为古怪。 [英] ToggleButtons as actionBar custom view behaving weirdly.

查看:172
本文介绍了作为的ToggleButtons ActionBar的自定义视图行为古怪。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我的code设立的动作条。 (使用默认的系统动作条,安卓4.2 +)

Firstly, my code for setting up the actionBar. (Using default system actionBar, android 4.2+)

@Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.listing_group, menu);
        MenuItem listItem = menu.findItem(R.id.action_list);
        ToggleButton customActionIcon = (ToggleButton) getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
        listItem.setActionView(customActionIcon);
        customActionIcon.setTextOff("LIST");
        customActionIcon.setTextOn("LIST");
        customActionIcon.setTypeface(OswaldRegular());

        customActionIcon= (ToggleButton)getLayoutInflater().inflate(R.layout.custom_action_bar_icon_view, null);
        customActionIcon.setTextOff("MAP");
        customActionIcon.setTextOn("MAP");
        customActionIcon.setTypeface(OswaldRegular());
        MenuItem mapItem = menu.findItem(R.id.action_map);
        mapItem.setActionView(customActionIcon);
        return super.onCreateOptionsMenu(menu);
    }

布局XML的菜单:

Layout xml for Menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_list"
        android:showAsAction="always"
    />
    <item
        android:id="@+id/action_map"
        android:showAsAction="always"/>

</menu>

布局XML定制动作条按钮:

Layout xml for custom actionBar button:

<?xml version="1.0" encoding="utf-8"?>
<ToggleButton xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/action_bar_toggle_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="12sp"
    android:background="@color/transparent"
    android:textColor="@drawable/listing_group_activity_actionbar_text_color_selector">
</ToggleButton>

问题:

当这个操作栏首次加载屏幕,无论是按钮取消选择的颜色显示出来,但他们都表现出关的默认文本。当我点击其中之一,他们正确地改变颜色,他们将文本更改为一个我在onCreateOptionsMenu()设置。即,一个变成列表和一个变MAP。然后他们继续保持这种方式和功能将正常的ToggleButtons。我想他们开始用正确的文字表现,且固定后,我想其中的一个被默认选择。

When the screen with this action bar first loads, both the buttons show up in the deselected color, but they both show the default text of "Off". When I click one of them, they correctly change color AND they change the text to the one I set in onCreateOptionsMenu(). i.e. , one becomes LIST and one becomes MAP . And then they continue to stay that way and function as normal ToggleButtons would. I would like them to start with the correct text showing, and after that is fixed I want one of them to be selected by default.

对此事的任何帮助是非常AP preciated!谢谢!

Any help on the matter is much appreciated! Thanks!

推荐答案

固定。它。必须为残疾人一个启用一个,setChecked(假)做customActionIcon.setChecked(真),他们开始用正确的文本和正确的状态

Fixed. it. Had to do customActionIcon.setChecked(true) for the enabled one and setChecked(false) for the disabled one and they start with the right text and right state

这篇关于作为的ToggleButtons ActionBar的自定义视图行为古怪。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆