创建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
无关.它与宽度与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>
gravity
和layout_gravity
都不会影响开关的位置 - 它向右对齐。可以用LinearLayout
替换FrameLayout
,结果将是相同的。要理解为什么会发生这种情况,应该尝试在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>
希望对你有帮助