2

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> 

答えて

2

それはバグのように見えるかもしれませんが、問題は同様Switchのために再現されるよう、実際にそれが(SwitchCompatとは関係ありません。 をしかし、それはまた、PercentRelativeLayoutとは関係ありません。とにも関係していません。RelativeLayoutに それは(私の知る限り)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もスイッチの位置に影響を与えません - 右に揃えられます。 FrameLayoutLinearLayoutと置き換えることができ、結果は同じになります。 Switch/SwitchCompatソースコードで回答を見つけようとする理由を理解するには(申し訳ありませんが、私はそうしようとしませんでした)

問題を解決するには、 with with a hack:いくつかのレイアウトでSwitchCompatをラップします。 wrap_contentSwitchCompatとしてください。

<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> 

は、それが

を役に立てば幸い
関連する問題