当前位置: 代码迷 >> Android >> 开关元件未在分配的空间内水平居中对齐
  详细解决方案

开关元件未在分配的空间内水平居中对齐

热度:36   发布时间:2023-08-04 09:47:51.0

创建PercentRelativeLayout ,我注意到尽管设置了属性,但SwitchCompat控件仍未与中心水平对齐。 为了解决此问题可以做什么? 我尝试使用android:layout_centerHorizontal="true" ,但这似乎不起作用。

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="10"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
    <ImageView
        android:id="@+id/imageView1"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_image1" />

    <android.support.v7.widget.SwitchCompat
        android:id="@+id/switch_map_emiratesairline_emiratesgreenwichpeninsula"
        app:layout_widthPercent="20%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:theme="@style/Theme.AppCompat.Light"
        android:background="@android:color/transparent"
        android:layout_toEndOf="@id/imageView1"/>

    <ImageView
        android:id="@+id/imageView2"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_image2"
        android:layout_toEndOf="@id/switch_tgl"/>
</android.support.percent.PercentRelativeLayout>

它可能看起来像个错误,但实际上与SwitchCompat不相关(因为Switch也重现该问题。但它也不与PercentRelativeLayout 。甚至与RelativeLayout不相关。它与Switch with宽度与wrap_content不同(据我所知)。

简单的例子:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center">

    <android.support.v7.widget.SwitchCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"/>
</FrameLayout>

gravitylayout_gravity都不会影响开关的位置-它会向右对齐。 可以用LinearLayout替换FrameLayout ,结果将是相同的。 要了解为什么会发生这种情况,应该尝试在Switch / SwitchCompat源代码中找到答案(对不起,我没有尝试过...)

因此,要解决您的问题,我唯一能想到的就是hack:用某些布局包装SwitchCompat 并使用wrap_content作为SwitchCompat宽度。

<android.support.percent.PercentRelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="10"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp">
    <ImageView
        android:id="@+id/imageView1"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/avatar" />

    <FrameLayout
        android:id="@+id/switch_tgl"
        app:layout_widthPercent="20%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:layout_toRightOf="@id/imageView1">
        <android.support.v7.widget.SwitchCompat
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.AppCompat.Light"
            android:background="@android:color/transparent" />
    </FrameLayout>

    <ImageView
        android:id="@+id/imageView2"
        app:layout_widthPercent="40%"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:src="@drawable/avatar"
        android:layout_toRightOf="@id/switch_tgl"/>
</android.support.percent.PercentRelativeLayout>

希望能帮助到你

  相关解决方案