问题描述
创建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>
1楼
它可能看起来像个错误,但实际上与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>
gravity
和layout_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>
希望能帮助到你