如何添加一个转投Android行动起来吧? [英] How to add a switch to android action bar?

查看:115
本文介绍了如何添加一个转投Android行动起来吧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想添加一个按钮来切换类似软糖本机的外观。 (蓝色/灰色开关在视图的顶部)

I would like to add a button switch similar to jellybean native look. (Blue/gray switch at the top of the view)

文档展示了如何创建一个菜单出现或添加图标,但它并没有说,如何添加自定义元素。例如。的开关。 <一href="http://developer.android.com/guide/topics/ui/actionbar.html">http://developer.android.com/guide/topics/ui/actionbar.html

Documentation shows how to create a menu there or add icons, but it does not say, how to add a custom elements. eg. a switch. http://developer.android.com/guide/topics/ui/actionbar.html

推荐答案

抱歉回复晚了。希望你找到了答案。

Sorry for replying late. Hope you found the answer.

创建用于开关一个布局。自定义布局菜单应该永远是RelativeLayout的

Create a layout for the switch. Custom layouts for menu should always be "RelativeLayout"

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Switch
        android:id="@+id/switchForActionBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</RelativeLayout>

然后,在你mainmenu.xml添加的项目如下:

Then, in your mainmenu.xml add the item as follows

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/myswitch"
        android:title=""
        android:showAsAction="always"
        android:actionLayout="@layout/switch_layout"
    />   
</menu>

而在你的活动中,夸大了menu.xml文件,你总是这样

And in your activity, inflate the menu.xml as you always do

    getMenuInflater().inflate(R.menu.mainmenu, menu);
    return true;

希望这有助于!

Hope this helps!

这篇关于如何添加一个转投Android行动起来吧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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