【问题标题】:Switch of Linear Layout not working线性布局开关不起作用
【发布时间】:2023-08-26 04:48:01
【问题描述】:

My switch in linear layout :

The switch suppose to be like this :

这些是我的代码:

<Switch
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:checked="true"
        android:onClick="onShowMiddlePoint"
        android:text="@string/showMiddlePoint" />

    <Switch
        android:id="@+id/switch2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:text="@string/showEyePoints"
        android:checked="false"
        android:onClick="onShowEyePoints" />

【问题讨论】:

  • 用问题分享你的整个 layout.xml

标签: android android-layout uiswitch


【解决方案1】:
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="280dp"
            >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="XXX"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="YYY"
                />


        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

            <Switch
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </LinearLayout>


</RelativeLayout>

【讨论】:

    【解决方案2】:

    试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="horizontal">
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/margin_10"
        android:layout_marginLeft="@dimen/margin_20"
        android:text="Demos"/>
    
    
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_marginTop="@dimen/margin_48"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
    
    <Switch
        android:id="@+id/simpleSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="switch"
        android:layout_marginTop="@dimen/margin_10"
        android:layout_centerHorizontal="true"
        android:textOn="On"
        android:textOff="Off"
        android:textColor="#f00"
        android:padding="10dp"
        android:gravity="center"
        android:textSize="25sp"
        /><!-- black background color-->
    
    <Switch
        android:id="@+id/simple_Switch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="switch"
        android:layout_centerHorizontal="true"
        android:textOn="On"
        android:textOff="Off"
        android:textColor="#f00"
        android:padding="10dp"
        android:gravity="center"
        android:textSize="25sp"
        /><!-- black background color-->
    </LinearLayout>
    

    我觉得对你有帮助

    【讨论】:

    • 我无法解析像 @dimen/margin_10 这样的维度符号。任何的想法?这是我的 dimens.xml 文件: 16dp16dp10dp