简体   繁体   English

RelativeLayout中的TextView对齐不正确

[英]TextView in RelativeLayout improper alignment

I need to implement the following UI: 我需要实现以下用户界面: 在此处输入图片说明

For this, I am using the following layout: 为此,我使用以下布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white">
    <ImageView
        android:id="@+id/news_image0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="@dimen/news_cell0_imageview_min_height"
        android:adjustViewBounds="true"
        android:scaleType="fitXY" />
    <RelativeLayout
        android:id="@+id/news0_cell_image_overlay_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/news_image0"
        android:layout_alignTop="@+id/news_image0"
        android:layout_alignLeft="@+id/news_image0"
        android:layout_alignStart="@+id/news_image0"
        android:layout_alignEnd="@+id/news_image0"
        android:layout_alignRight="@+id/news_image0"
        android:background="@drawable/news_cell0_overlay_gradient">
        <TextView
            android:id="@+id/news_title0"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:gravity="center_vertical|start"
            android:layout_alignParentBottom="true"
            android:maxLines="3"
            android:ellipsize="end"
            android:textColor="@color/colorNewsCellType0TitleText"
            android:layout_margin="@dimen/news_cell0_textview_margin"
            android:textSize="@dimen/news_cell0_title_text_size"
            android:typeface="monospace" />
    </RelativeLayout>
</RelativeLayout>

Into the ImageView I am loading image using Picasso from a URL. 进入ImageView我正在使用来自URL的Picasso加载图像。 I am getting output correctly in most of the devices, but in some of the devices running android less than Lollipop, this shows up like the screenshot below. 我在大多数设备上都能正确获得输出,但是在一些运行Android的设备(而不是Lollipop)上,这些输出就像下面的屏幕截图所示。 The text shows up at the top, instead of bottom. 文本显示在顶部,而不是底部。 But I have set android:layout_alignParentBottom="true" for the TextView. 但是我已经为TextView设置了android:layout_alignParentBottom="true" The RelativeLayout is rendered correctly, and it fits the image(it has a background gradient, which can clearly be seen at the bottom of the image). RelativeLayout正确呈现,并且适合图像(它具有背景渐变,可以在图像底部清晰看到)。

在此处输入图片说明

What is causing this problem and how can I solve this? 是什么导致此问题,我该如何解决?

You can try with FRAMELAYOUT 您可以尝试使用FRAMELAYOUT

FrameLayout is designed to block out an area on the screen to display a single item. FrameLayout旨在遮挡屏幕上的某个区域以显示单个项目。 Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. 通常,应使用FrameLayout来保存单个子视图,因为在子视图彼此不重叠的情况下,难以以可扩展到不同屏幕尺寸的方式组织子视图。

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">

   <ImageView 
      android:src="@drawable/ic_launcher"
      android:scaleType="fitCenter"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"/>

   <TextView
      android:text="Hi.."
      android:textSize="17sp"
      android:textStyle="bold"
      android:layout_height="fill_parent"
      android:layout_width="fill_parent"
      android:gravity="center|bottom"/>
</FrameLayout>

You can try this layout, 您可以尝试这种布局,

<?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:background="@android:color/white">

    <ImageView
        android:id="@+id/news_image0"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:minHeight="100dp"
        android:scaleType="fitXY"/>

    <TextView
        android:id="@+id/news_title0"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/news_image0"
        android:layout_centerHorizontal="true"
        android:ellipsize="end"
        android:maxLines="3"
        android:padding="12dp"
        android:text="fsdsdfdsdsfsdfsdf"
        android:textColor="#f00"
        android:typeface="monospace"/>
</RelativeLayout>

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

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