【问题标题】:Centering text under an imageview in android在android中的imageview下居中文本
【发布时间】:2014-02-14 23:57:23
【问题描述】:

我需要在图像下放置一些文本。使用我当前的代码,文本位于图像下方,但上边距相当大,并且偏离图像中心(向右移动约 10dp)。我想让我的文本直接位于图像下方,以图像为中心。

我尝试过android:layout_centerInParent="true",但这只会将我的文本放在屏幕中间。

这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScanResults" >

<ImageView
    android:id="@+id/productLogo"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/wedge_color"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" />

<TextView
    android:id="@+id/productTitle"
    android:layout_width="140dp"
    android:layout_height="wrap_content"
    android:layout_below="@id/productLogo"
    android:gravity="center"
    android:text="Fluffer Nutter Sandwich"
    android:textSize="14sp" />

</RelativeLayout>

感谢您的帮助。

【问题讨论】:

    标签: android textview imageview centering


    【解决方案1】:

    对于 ImageView,您将 "100dp" 赋予 android:layout_width 但对于 TextView,你给了 140dp...这就是 TextView 没有正确对齐的原因。现在为TextView设置android:layout_width="100dp",问题就解决了。

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".ScanResults" >
    
        <ImageView
            android:id="@+id/productLogo"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@drawable/wedge_color"
            android:paddingLeft="10dp"
            android:paddingRight="10dp" />
    
        <TextView
            android:id="@+id/productTitle"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_below="@id/productLogo"
            android:gravity="center"
            android:text="Fluffer Nutter Sandwich"
            android:textSize="14sp" />
    
    </RelativeLayout>
    

    【讨论】:

    • 成功了,我受够了,我忘了我已经改变了图像的尺寸......谢谢!
    【解决方案2】:

    android:gravity="center_horizontal|center_vertical" 添加到 textview 以使其位于布局的中心。

    <TextView
        android:id="@+id/productTitle"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/productLogo"
        android:gravity="center_horizontal|center_vertical"
        android:text="Fluffer Nutter Sandwich"
        android:textSize="14sp" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多