Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android中图像视图下的居中文本_Android_Textview_Imageview_Centering - Fatal编程技术网

android中图像视图下的居中文本

android中图像视图下的居中文本,android,textview,imageview,centering,Android,Textview,Imageview,Centering,我需要在图像下放置一些文本。在我当前的代码中,文本位于图像下方,但上边距相当大,并且偏离图像中心(向右移动约10dp)。我想让我的文字直接位于图像下方,以图像为中心 我已经尝试过 Android:LayOutCultInPrime=“true”,但这只是将我的文本放在屏幕中间。 这是我的密码: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://sche

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

我已经尝试过<代码> Android:LayOutCultInPrime=“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:gravity=“center_horizontal | center_vertical”添加到文本视图中,使其位于布局的中心

<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" />

对于图像视图,您向android:layout_width提供了100dp,但对于文本视图,您提供了140dp…这就是文本视图未正确对齐的原因。现在为文本视图设置
android:layout\u 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>

就是这样,我受够了,忘了我已经改变了图像的尺寸……谢谢!