0

我在Toolbarandroid.support.v7.widget.SwitchCompat上添加了一个开关。这是我的代码:

SwitchCompat 布局文件toolbar_switch.xml

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

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/toolbar_switch"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text=""/>

</RelativeLayout>


menu_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity">

<item
    android:id="@+id/action_clipboard_switch"
    android:title=""
    app:actionLayout="@layout/toolbar_switch"
    android:checkable="true"
    android:enabled="true"
    app:showAsAction="always"/>


这段代码只是向我展示了工具栏上的一个开关。但是当我点击开关时没有任何反应。我什至不切换。
我还向 onClickListener 添加了一个 Toast,但它不起作用。帮助!!!

编辑Switch 和 SwitchCompat 都不起作用!

4

1 回答 1

0

我自己想出来的,我现在觉得很愚蠢。

我在工具栏下方声明了一个 TextView 并声明了它的layout_heightand 。layout_widthmatch_parent

    <TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textIsSelectable="true"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:textSize="32sp"
    android:textStyle="bold"
    android:text="@string/text"/>

TextView 填满了整个屏幕,因此开关无法正常工作。

于 2015-11-30T19:03:37.037 回答