java - android:layout_alignParentTop ="true"和 android:layout_centerVertical ="true"不设置 imageView 在屏幕顶部居中

标签 java android xml android-imageview android-relativelayout

我有一个非常简单的布局,我试图将 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_centerHorizo​​ntal="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/

相关文章:

java - @Cacheable 和 @CachePut 不同的返回类型

java - 访问 for 循环内创建的字符串变量

android - 提前预留应用市场网址

xml - 用 Ant 替换多行文本

xml - 在 Spring-Boot Intro 之后, "Unable to find a suitable main class, please add a ' mainClass 的属性“

java - args.length 和命令行参数

android - 信号 11 (SIGSEGV),代码 1 (SEGV_MAPERR) base.apk

android - 如何在 Android Studio 2.2 上刷新预览?

c# - 将 XML 反序列化为对象 - XML 文档 (0, 0) 中存在错误

java - 将 ByteBuffer 流转换为 Rx 中的行的有效方法