【问题标题】:android:layout_alignParentTop="true" and android:layout_centerVertical="true" do not set the imageView centered at the the top of the screenandroid:layout_alignParentTop="true" 和 android:layout_centerVertical="true" 不将imageView设置在屏幕顶部居中
【发布时间】:2014-01-21 22:13:12
【问题描述】:

我有一个非常简单的布局,我试图将 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>

【问题讨论】:

    标签: java android xml android-imageview android-relativelayout


    【解决方案1】:

    我正在尝试将 imageView 置于屏幕中间但与顶部对齐..

    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>
    

    希望对您有所帮助。

    【讨论】:

    • 确实,android:layout_alignParentTop="true" 不是必须的,它默认对齐到父级的顶部。
    猜你喜欢
    • 2012-08-20
    • 1970-01-01
    • 2022-07-12
    • 1970-01-01
    • 2017-03-18
    • 2010-09-08
    • 1970-01-01
    • 2015-01-23
    • 2013-10-13
    相关资源
    最近更新 更多