android switch控件的大小,关于Android Action Bar 上的 Switch控件

博主想在Android的Action Bar上添加Switch按钮,按特定方法实现按钮后,无法使用onCheckedChanged检测按钮开启状态。博主给出了实现代码,并猜测可能是因为MainActivity的setContentView设置问题导致无法通过findViewByid和onCheckedChanged检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

是这样的:

因为要在Action Bar 上弄个 Switch 按钮,所以我照

http://stackoverflow.com/questions/12107031/how-to-add-a-switch-to-and...

的方法做了个.

现在做到按钮了,但却不能使用onCheckedChanged来验测按钮的开启

我该怎样做?

这是我实现Action Bar 上 放Switch 的代码

mainmenu.xml

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

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity">

android:title="@string/action_settings"

android:orderInCategory="100"

app:showAsAction="never"/>

android:id="@+id/myswitch"

android:title=""

app:showAsAction="always"

app:actionLayout="@layout/menurelaty"

/>

menurelaty.xml

android:orientation="vertical"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/switchForActionBar"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text=""

android:layout_centerVertical="true"

android:checked="false"

/>

我在想:会不会是因为MainActivity上的setContentView是main_activity.xml而不是实现Switch的menurelaty.xml才不能以findViewByid然后.onCheckedChanged 来验测按钮的开启

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值