android 垂直的开关_如何在Android操作栏中添加开关?

如何在Android操作栏中添加开关?

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

文档显示了如何在此处创建菜单或添加图标,但并未说明如何添加自定义元素。 例如。 一个开关。[http://developer.android.com/guide/topics/ui/actionbar.html]

5个解决方案

91 votes

为开关mainmenu.xml创建布局。菜单的自定义布局应始终为RelativeLayout

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/switchForActionBar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

然后,在您的mainmenu.xml中,添加以下内容

android:id="@+id/myswitch"

android:title=""

android:showAsAction="always"

android:actionLayout="@layout/switch_layout"

/>

在您的活动中,像往常一样给mainmenu.xml充气

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

return true;

Ezequiel answered 2020-02-04T18:31:57Z

35 votes

最终弄清楚了我的问题:对于使用新AppCompat的用户,应该在开关布局上使用android.support.v7.widget.SwitchCompat而不是Switch ...否则,它不会显示在ActionBar上(假设您也在使用AppCompat ActionBar) ,好吧,actionLayout属性不起作用,必须在代码中进行设置。

android:id="@+id/switchView"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="horizontal">

android:id="@+id/switchForActionBar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:text="" />

然后在代码中设置布局:

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem item = menu.findItem(R.id.on_off_switch);

item.setActionView(R.layout.on_off_switch);

return true;

}

Vu Nguyen answered 2020-02-04T18:32:22Z

30 votes

如果窗口小部件未出现在操作栏中,则可能是因为您正在将appCompat用于操作栏中。 要解决此问题,请在menu.xml中的“ showAsAction”和“ actionLayout”前面将“ android:”切换为“ app:”

使用app:代替android将项目添加到xml:

android:id="@+id/myswitch"

android:title=""

app:showAsAction="always"

app:actionLayout="@layout/switch_layout"

/>

制作用于“ app:actionLayout”的布局

switch_layout

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

android:layout_width="fill_parent"

android:layout_height="match_parent"

android:orientation="horizontal" >

android:id="@+id/switchAB"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true"

/>

像往常一样在ActionBarActivity中添加菜单

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

return true;

如果未显示该开关,则该开关应显示在您的操作栏中。

Neil M. answered 2020-02-04T18:33:05Z

5 votes

Ezequiel给出的解决方案很棒并且可行。 这是另一种方法:

定义您的自定义布局:

android:id="@+id/actionbar_switch"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="" />

以编程方式对其进行充气:

ActionBar actionBar = getSupportActionBar();

actionBar.setCustomView(R.layout.actionbar_top);

actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_CUSTOM);

...

Switch button = (Switch) findViewById(R.id.actionbar_switch);

Fabricio PH answered 2020-02-04T18:33:34Z

3 votes

对于那些想要添加的人,

将更改侦听器检查到同一交换机

科特林

override fun onCreateOptionsMenu(menu: Menu?): Boolean {

menuInflater.inflate(R.menu.menu_main, menu)

val item = menu!!.findItem(R.id.my_switch_item)

item.setActionView(R.layout.switch_layout)

val mySwitch = item.actionView.findViewById(R.id.switch_id)

mySwitch.setOnCheckedChangeListener(object : CompoundButton.OnCheckedChangeListener{

override fun onCheckedChanged(p0: CompoundButton?, isChecked: Boolean) {

// do what you want with isChecked

}

})

return true

}

爪哇

@Override

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.menu_main, menu);

MenuItem item = menu.findItem(R.id.my_switch_item);

item.setActionView(R.layout.switch_layout);

Switch mySwitch = item.getActionView().findViewById(R.id.switch_id);

mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

// do something based on isChecked

}

});

return true;

}

附言 您可以更改对Switch或SwitchCompat的引用

gprathour answered 2020-02-04T18:34:11Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值