我有一个非常简单的布局,我试图将 imageView 置于屏幕中间,但与顶部对齐。我尝试使用以下内容,但它似乎不起作用,我不确定为什么: imageView 出现在左上角,而不是居中并与顶部对齐。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
最佳答案
I'm attempting to center an imageView in the middle of the screen but aligned to the top..
在ImageView
中使用android:layout_centerHorizontal="true"
而不是android:layout_centerVertical="true"
。因此,以下应该是您的 XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="@+id/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/backspace_icon" />
</RelativeLayout>
希望有帮助。
关于java - android:layout_alignParentTop ="true"和 android:layout_centerVertical ="true"不设置 imageView 在屏幕顶部居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21270155/