After creating a PercentRelativeLayout, I noticed that the SwitchCompat control does not align to the center horizontally despite settings properties. What can be done in order to resolve this issue? I tried using android:layout_centerHorizontal="true", but this doesn't seem to work.
<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>
It might look like a bug, but actually it doesn't relate to
SwitchCompat(as issue is reproduced forSwitchas well. But it also doesn't relate toPercentRelativeLayout. And even doesn't relate toRelativeLayout. It relates to Switch with width different fromwrap_content(as far as I can see).Simple example:
Neither
gravitynorlayout_gravityhas effect on switch's position - it is aligned to the right. One can replaceFrameLayoutwithLinearLayoutand result will be the same. To understand why this happens one should try to find answer in Switch/SwitchCompat source code (sorry, I haven't tried to do so...)So, to resolve your issue the only thing I could come up with is a hack: wrap
SwitchCompatwith some layout. And usewrap_contentasSwitchCompatwidth.Hope it helps