如何添加一个转投Android行动起来吧?来吧、转投、行动、Android

2023-09-12 21:58:21 作者:原来你只是路过

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

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

文档展示了如何创建一个菜单出现或添加图标,但它并没有说,如何添加自定义元素。例如。的开关。 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!