繁体   English   中英

RelativeLayout- android:layout_centerHorizo​​ntal =“true”和android:layout_centerVertical =“true”不要将imageview居中

[英]RelativeLayout- android:layout_centerHorizontal=“true” and android:layout_centerVertical=“true” dont center the imageview

出于一些奇怪的原因android:layout_centerHorizontal="true"android:layout_centerVertical="true"不要将ImageView放在中心似乎不是我的ImageView中心

有什么建议?

图像卡在屏幕的右上角,并且没有按预期居中。

资源:

*<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8sp"
    android:layout_marginLeft="8sp"
    android:layout_marginRight="8sp"
    android:layout_marginTop="8sp"
    android:background="@drawable/background"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/emblem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            android:src="@drawable/apn_app_logo" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/start2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8sp"
        android:layout_toRightOf="@id/start" >

        <Button
            android:id="@+id/go_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@drawable/apn_app_go_button" />

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/go_button"
            android:layout_marginRight="8sp"
            android:layout_marginTop="8sp"
            android:gravity="center"
            android:text="@string/start_text"
            android:textColor="#000000"
            android:textSize="18sp" />
    </RelativeLayout>

</RelativeLayout>*

centerHorizontal移动到相对布局。 目前,您将图像置于图像视图中心(基本上您将图像置于视图中心),您想要的是使内容居中的相对布局。

尝试:

<RelativeLayout
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_centerHorizontal="true">

        <ImageView
            android:id="@+id/emblem"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            android:src="@drawable/apn_app_logo" />
    </RelativeLayout>

另一种选择是:

<RelativeLayout
        android:id="@+id/start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
       >

        <ImageView
            android:id="@+id/emblem"
            android:layout_width="match_parent" //change to match_parent
            android:layout_height="match_parent" //change to match_parent
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:adjustViewBounds="true"
            android:scaleType="fitStart"
            android:src="@drawable/apn_app_logo" />
    </RelativeLayout>

第二个选项设置的宽度和高度imageView匹配relativeView ,这将允许您使用centerHorizontalcenterVerticalimageView

你不能在LinearLayout中使用android:layout_below。 您可以将这两个项目放在RelativeLayout中,或者您可以将LinearLayout从“水平”更改为“垂直”,如下所示:

<LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM