动作栏抽屉切换自定义图标 [英] Action Bar Drawer Toggle Custom Icon
问题描述
我正在尝试使用操作栏抽屉开关,但我希望它显示的不仅仅是菜单图标.我希望它在自定义菜单图标的右上角显示通知计数,但是我仍然希望操作栏抽屉开关在需要时显示后退箭头.
I'm trying to use the action bar drawer toggle, but I would like it to display something more than just the menu icon. I want it to display a notification count in the top right corner of my custom menu icon, but I still want the action bar drawer toggle to display the back arrow when needed.
因此,我首先尝试查看是否可以在操作栏抽屉开关上显示自定义布局(包含用于菜单图标的图像视图和用于通知计数的文本视图),但没有成功.
Therefore, I first tried to see if I could display a custom layout (containing an imageview for my menu icon and a textview for my notification count) on the action bar drawer toggle but whithout success.
然后我尝试创建一个包含自定义菜单图标的菜单项.单击此菜单项后,我打开/关闭导航抽屉,但无法在菜单图标上显示包含我的通知的文本视图.
I then tried to create a menu item containing my custom menu icon. When this menu item is clicked, I open/close the navigation drawer, but I can't manage to display a textview containing my notification on the menu icon.
感谢任何帮助/想法:)
Any help/idea is appreciated :)
推荐答案
尝试以下方式:
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("hi");
actionBar.setBackgroundDrawable(getResources().getDrawable(
R.drawable.actionbar_back_color));
actionBar.setIcon(getResources().getDrawable(
R.drawable.image_crush_home_drawar_icon));
actionBar.setCustomView(R.layout.actionbar_right_two_icon);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayUseLogoEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(false);
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/actionbar_back_color"
android:orientation="horizontal"
android:weightSum="6" >
<LinearLayout
android:id="@+id/line_search"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_action_"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/icon_action" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="3"
android:orientation="vertical" >
<TextView
android:id="@+id/actionbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left"
android:singleLine="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/actionbar_text_color" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_search"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_crush_home_search_icon" />
</LinearLayout>
<LinearLayout
android:id="@+id/line_post_Message"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image_crush_home_message_icon" />
</LinearLayout>
</LinearLayout>
这篇关于动作栏抽屉切换自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!