简体   繁体   English

将TextView与RelativeLayout内ImageView的底部对齐

[英]Align TextView to the bottom of an ImageView inside a RelativeLayout

I have the following layout: 我有以下布局:

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

    <ImageView
        android:id="@+id/img"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:scaleType="centerCrop"
        android:src="@drawable/miclubpyme_backgroundl" />

    <TextView
        android:id="@+id/txtStoreName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="I am a text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

I would like to have the TextView to align to the bottom of the ImageView, but not outside it. 我想让TextView对齐ImageView的底部,但不在其外部。 Adding the layout_alignParentBottom makes the RelativeView to occupy the full height of the screen. 添加layout_alignParentBottom可使RelativeView占据屏幕的整个高度。

Here is an image exemplyfing this: 这是示例图片:

在此处输入图片说明 `` ``

Any help would be greatly appreciated. 任何帮助将不胜感激。 Thanks! 谢谢!

You would have to nest your ImageView and your TextView inside a ViewGroup which has the same height as your ImageView. 您必须将ImageView和TextView嵌套在与ImageView高度相同的ViewGroup中。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/img"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/miclubpyme_backgroundl" />

        <TextView
            android:id="@+id/txtStoreName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="I am a text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

    </FrameLayout>

</RelativeLayout>

您正在TextView上寻找android:align_bottom="@+id/img" ,而不是android:align_parentBottom="true"

在您的textview中尝试以下android:layout_alignBottom =“ @ id / img”`

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

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