Mac*_*ver 2 xml android android-layout android-relativelayout android-percent-library
创建 后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>
Run Code Online (Sandbox Code Playgroud)
    它可能看起来像一个错误,但实际上它与SwitchCompat( 无关,因为问题也被重现Switch。但它也与 无关PercentRelativeLayout。甚至与 无关RelativeLayout。它与宽度不同于 ( 的 Switch 相关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>
Run Code Online (Sandbox Code Playgroud)
也不影响开关的位置gravity-layout_gravity它向右对齐。可以FrameLayout用替换LinearLayout,结果是一样的。要理解为什么会发生这种情况,应该尝试在 Switch/SwitchCompat 源代码中找到答案(抱歉,我还没有尝试这样做......)
因此,为了解决您的问题,我唯一能想到的就是一个技巧: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>
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你