3

出于某种奇怪的原因 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>*
4

2 回答 2

3

将 移动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

于 2013-09-20T17:39:43.863 回答
1

您不能在 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" >
于 2013-09-20T17:28:05.050 回答