在相对布局中居中ImageView

7

我有一个相对布局里面包含一张图片和一个文本视图。这张图片和文本视图各自占据屏幕宽度的50%。我该如何让它们都居中显示?我尝试了很多方法,但不知道哪里出错了...

这是我的代码:

        <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100">
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="50"
            android:layout_centerHorizontal="true">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textViewEventImage"
                android:adjustViewBounds="true" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:background="@color/eventNameBackground"
                android:id="@+id/textViewEventName"
                android:textColor="@android:color/white"
                android:textSize="15sp"/>
        </RelativeLayout>
    </LinearLayout>

enter image description here


你想让你的图片位于屏幕中央吗? - Akanksha Hegde
4个回答

16

尝试使用这段代码,它将符合您的要求

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.5"
        android:background="#ff0000" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="180dp"
        android:layout_height="120dp"
        android:layout_centerInParent="true"
        android:gravity="center_vertical"
        android:src="@drawable/americanexpress" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_below="@+id/imageView1"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="16dp"
        android:text="Your product" />
</RelativeLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.5"
    android:background="#00ff00" />

</LinearLayout>

5

尝试像这样

imageview放置在此处

centerinparent ="true"

并且textview还需要添加centerinparent="true"属性。

同时,在textview中添加一个属性:

As
android: layout_below="@+id/imageview id"

那不起作用了。ImageView 仍然在屏幕左侧,而 TextView 在 ImageView 中间。 - jak111

1
也许这会有所帮助。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    <ImageView
        android:layout_width="xxxdp"
        android:layout_height="xxxdp"
        android:src="@drawable/someimage"
        />
    <TextView
        android:layout_width="xxxdp"
        android:layout_height="xxxdp" 
        android:text="fweqf"/>
    </LinearLayout>

</RelativeLayout>

我必须使用相对布局,因为我想在ImageView上显示文本... - jak111
我的答案将会产生一个结果,就像你所提到的那张图片一样,它使用了一个RelativeLayout来包含一个LinearLayout视图。而你所说的“在ImageView上方显示文本”是什么意思呢?你是想让你的图片看起来像是文本的背景吗?我很想知道更多关于你想要做什么的细节。 - Simo

0
如果您想让ImageView和TextView居中显示,请使用以下代码。
<?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="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/textViewEventImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textViewEventName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewEventImage"
        android:layout_centerInParent="true"
        android:text="There is Your Text"
        android:textSize="15sp" />
</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接