如何将TextView居中于父级ImageView上?

3

我尝试了几种方法,但每次都失败了。

我想要实现的是在ImageView上将TextView居中(水平和垂直方向),但是使用android:layout_centerInParent="true"命令会导致垂直和水平居中于整个区域,而不是在ImageView上。请帮我将父元素附加到TextView或其他解决方法。

这是我的xml代码:

 <ImageView
    android:id="@+id/imageView1"
    android:layout_width="170dp"
    android:layout_height="46dp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:contentDescription="Your Height BG"
    android:src="@drawable/textareabg" />

     <TextView
    android:id="@+id/textView00"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"

    android:text="Your Height"
    android:textColor="#d88b6d"
    android:textSize="20sp" />

我会使用RelativeLayout作为父布局,并将其背景设置为ImageView的背景,但如果您不喜欢这种方式,也可以尝试其他方法。 - Spurdow
你的 ImageView 高度和宽度固定吗? - Umer Kiani
6个回答

2

使用ConstraintLayout可以轻松实现。Android最近推出了ConstraintLayout。它允许我们使用“约束”来定义布局中不同视图之间的位置关系,从而布置子视图。它类似于RelativeLayout,但比后者更强大,因为ConstraintLayout将视图层次结构减少到更大程度。现在回到你的问题,以下是可以完成工作的示例xml代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto">

   <ImageView
       android:id="@+id/image"
       android:layout_width="200dp"
       android:layout_height="200dp"
       android:src="@mipmap/ic_launcher"
       app:layout_constraintRight_toRightOf="parent"
       app:layout_constraintLeft_toLeftOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintVertical_bias="0.1"/>

   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="Image"
       android:textSize="20sp"
       app:layout_constraintRight_toRightOf="@+id/image"
       app:layout_constraintLeft_toLeftOf="@+id/image"
       app:layout_constraintTop_toTopOf="@+id/image"
       app:layout_constraintBottom_toBottomOf="@+id/image"/>

</android.support.constraint.ConstraintLayout>

设备上的输出视图

device_output

使用app:layout_constraintHorizontal_biasapp:layout_constraintVertical_bias,您可以将TextView放置在ImageView的任何位置。默认情况下,这些值将设置为0.5。因此,默认情况下它将居中对齐,如果我们不指定任何值。

当您向视图的两侧添加约束(且相同维度的视图大小为“固定”或“包装内容”)时,默认情况下该视图将在两个锚点之间居中。

注意:偏置属性仅在为边界指定了约束条件(例如垂直偏差的顶部和底部,水平偏差的左侧和右侧)时才起作用。

更多关于ConstraintLayout的信息

ConstraintLayout

样例项目

关于 ConstraintLayout 的博客


0

对我来说似乎没有起作用。请记住我是新手,可能是我没有在这里说明的做错了什么,所以如果您能指导一下就太好了。 - Bronsoner

0

尝试这种方法,希望能帮助您解决问题。

<FrameLayout
   android:layout_width="wrap_content"
   android:layout_height="wrap_content">
   <ImageView
      android:id="@+id/imageView1"
      android:layout_width="170dp"
      android:layout_height="46dp"
      android:layout_marginLeft="10dp"
      android:layout_marginTop="10dp"
      android:contentDescription="Your Height BG"
      android:src="@drawable/textareabg" />

   <TextView
      android:id="@+id/textView00"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:text="Your Height"
      android:textColor="#d88b6d"
      android:textSize="20sp" />
</FrameLayout>

0

这是我会做的事情

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="170dp"
    android:layout_height="46dp"
    android:layout_centerHorizontal="false"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:contentDescription="Your Height BG" />

 <TextView
     android:id="@+id/textView00"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/imageView1"
     android:layout_alignTop="@+id/imageView1"
     android:layout_centerHorizontal="true"
     android:layout_centerInParent="true"
     android:layout_centerVertical="true"
     android:layout_marginLeft="30dp"
     android:layout_marginTop="10dp"
     android:text="Your Height"
     android:textColor="#d88b6d"
     android:textSize="20sp" />


0
你需要做的是使用FrameLayout。文档中说:
FrameLayout被设计用来在屏幕上阻挡出一个区域来显示单个项目。通常,应该使用FrameLayout来容纳一个子视图,因为如果不重叠,很难以一种可扩展到不同屏幕尺寸的方式组织子视图。但是,您可以将多个子视图添加到FrameLayout,并通过为每个子视图分配重力(使用android:layout_gravity属性)来控制它们在FrameLayout中的位置。 子视图按照堆栈的方式绘制,最近添加的子视图位于顶部。 FrameLayout的大小是其最大子视图的大小(加上填充),无论是否可见(如果FrameLayout的父级允许)。如果将setConsiderGoneChildrenWhenMeasuring()设置为true,则GONE状态的视图仅用于计算大小。
所以你需要类似以下伪代码布局:
<FrameLayout>
   <ImageView gravity="center">
   <TextView gravity="center">
</FrameLayout>

0

在您的布局下添加另一个相对布局,如下所示:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/imageView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

检查您想要的宽度和高度。例如,如果您想要图像的特定高度和宽度,请更改此相对布局的高度和宽度。

就这样...


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