【问题标题】:Android - Center Textview in ImageView of Relative LayoutAndroid - 在相对布局的 ImageView 中居中 Textview
【发布时间】:2015-02-04 07:13:00
【问题描述】:

我有一个包含 ImageView 的相对布局,并希望在活动布局 xml 文件中将 TextView 居中在 ImageView 的一侧。

这是图像视图,然后是我为 TextView 尝试过的内容

这是相对布局的内部:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="170dp"
    android:minWidth="170dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="33dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="33dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignStart="@+id/logoBackground"
    android:layout_marginLeft="43dp"
    android:layout_marginStart="43dp"
    android:layout_marginTop="65dp"/>

我觉得看起来还可以,但是 marginTop 是 65 不应该是 imageView 高度的一半吗?

提前感谢您的帮助。

【问题讨论】:

  • 如果您想为布局添加背景颜色。然后您也可以尝试将其添加到 Theme 中而不是 ImageView 中?

标签: java android xml android-layout


【解决方案1】:

它不起作用,因为您告诉 TextView 位于父级 RelativeLayout 的中心,但 ImageView 附加到父级的左上角。

一种方法是使用RelativeLayout 的alignXyz 属性将TextView 强制为ImageView 的确切大小。然后将重力属性设置为中心,这会使文本出现在 TextView 边界的中心。只要 ImageView 在所有维度上都大于 TextView,这应该可以工作。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_alignTop="@+id/logoBackground"
    android:layout_alignLeft="@+id/logoBackground"
    android:layout_alignRight="@+id/logoBackground"
    android:layout_alignBottom="@+id/logoBackground"
    android:gravity="center"/>

这对你有用吗?

【讨论】:

  • 不,我只是想将它设置为垂直居中,我不希望它与 ImageView 的大小相同
  • 是的,我明白了。这就是您设置 TextView 的重力的原因——因此文本绘制在视图的中间。您是说您还有其他项目也显示在 ImageView 上吗?在那种情况下,这种方法是行不通的,但你没有提到其他观点。
【解决方案2】:

为此,您必须使用Frame Layout

有关参考,请参阅此处 www.developer.android.com/reference/android/widget/FrameLayout.html

<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:layout_height="fill_parent"
   android:layout_width="fill_parent"/>

   <TextView
   android:text="Frame Text"
   android:textSize="30px"
   android:textStyle="bold"
   android:layout_height="fill_parent"
   android:layout_width="fill_parent"
   android:gravity="center"/>
</FrameLayout>

【讨论】:

  • 所以是框架布局而不是相对布局?
  • 是的。每当您想用另一个视图覆盖视图时。你必须使用Frame Layout
  • 不正确。 RelativeLayout 也可以用于覆盖视图。
  • 你可以这样做。但是Relative 布局的目的是仅以Relatively 的方式放置视图。
  • 是的,通常它们需要相对于父级,但覆盖在其他视图上。我们一直在使用它。说覆盖仅适用于 FrameLayout 是不正确的。基本上FrameLayout只是ViewGroup中最简单的一种,如果需要额外的定位控制,就使用RelativeLayout。
【解决方案3】:

我看到的一个问题是这一行:

android:layout_centerInParent="@+id/logoBackground"

layout_centerInParent 的值应该是 true 或 false。我猜它评估为假并且在左上角。如果将其设置为 true,它将在父级(相对布局)的中间居中。要在 ImageView 中居中,您需要使相对布局与 ImageView 的大小相同。

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

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:minHeight="170dp"
    android:minWidth="170dp"
    android:id="@+id/logoBackground"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="33dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="33dp"
    android:contentDescription="@string/content_description_useless"
    android:background="#ffffd0cd" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/capital_u"
    android:id="@+id/textView"
    android:textSize="30sp"
    android:layout_centerInParent="true"/>
</RelativeLayout>

【讨论】:

    【解决方案4】:

    在你的文本视图中试试这个:android:layout_centerInParent="true"

    因此:

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minHeight="170dp"
        android:minWidth="170dp"
        android:id="@+id/logoBackground"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="33dp"
        android:layout_marginStart="33dp"
        android:layout_marginTop="33dp"
        android:contentDescription="@string/content_description_useless"
        android:background="#ffffd0cd" />
    
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/capital_u"
        android:id="@+id/textView"
        android:textSize="30sp"
        android:layout_alignTop="@+id/logoBackground"
        android:layout_alignLeft="@+id/logoBackground"
        android:layout_alignStart="@+id/logoBackground"
        android:layout_marginLeft="43dp"
        android:layout_marginStart="43dp"
        android:layout_marginTop="65dp"
        android:layout_centerInParent="true"/>
    

    【讨论】:

      【解决方案5】:

      试试这个代码:

      <RelativeLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center">
      
          <ImageView
              android:scaleType="centerCrop"
              android:layout_width="30dp"
              android:layout_height="30dp"/>
      
          <TextView
              android:id="@+id/your_text_id"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textSize="14sp"
              android:layout_centerHorizontal="true"
              android:layout_centerVertical="true"
              android:text="1" />
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-10
        相关资源
        最近更新 更多