Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.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_Android Layout - Fatal编程技术网

Android 图像上的相对布局和文本

Android 图像上的相对布局和文本,android,android-layout,Android,Android Layout,我有一个自定义组件,它有一个ImageView,还有一个文本。我的问题是,如果在布局中设置文本centerHorizontal=true,则文本居中正常,但如果文本太长,则会在图像视图上重叠。如果我设置了layout\u toRightOf=“@+id/imageviewid”,则文本不会重叠,但文本看起来太接近图像视图。我希望文本居中,但不要与图像视图重叠。 我检查过,这是我尝试过的解决方案,但它不适合我 布局: <?xml version="1.0" encoding="utf-8"?

我有一个自定义组件,它有一个
ImageView
,还有一个文本。我的问题是,如果在布局中设置文本
centerHorizontal=true
,则文本居中正常,但如果文本太长,则会在图像视图上重叠。如果我设置了
layout\u toRightOf=“@+id/imageviewid”
,则文本不会重叠,但文本看起来太接近图像视图。我希望文本居中,但不要与图像视图重叠。

我检查过,这是我尝试过的解决方案,但它不适合我

布局:

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


<ImageView
    android:layout_width="40dp"
    android:layout_height="30dp"
    android:id="@+id/imagecolor"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_centerVertical="true"
    android:layout_marginLeft="15dp"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/textcolor"
    android:layout_centerVertical="true"
    android:gravity="center_horizontal"
    android:textColor="@color/white"
    android:layout_centerHorizontal="true"
    android:text="Placeholder"
    android:layout_toRightOf="@+id/imagecolor"/>

<Space
    android:layout_width="20dp"
    android:layout_height="60dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
</RelativeLayout>

您可以为文本视图提供从左侧到文本视图的边距,它始终与左侧图像视图保持一定距离

android:layout_marginLeft=“10dp” .... .....

您想要实现的目标能否请您给我们展示一张您想要实现的图片我需要文本居中,但不要与左侧的ImageView重叠。当前,如果文本太长,则会重叠。如果我设置torightof,它不会重叠,但是如果文本太短,它看起来太接近ImageView…一个更好的主意是将图像设置在TextView中,作为一个复合绘图-这也减少了视图计数,以获得更好的性能。我不能这样做,Frank。我需要图像视图和文本视图,并排。是的,但是我怎么知道边距的距离呢?如果文本很短,我不需要任何边距。如果它很长,我会需要它…但是多少钱?我不认为这是可行的。每次给5 dp的余量,我不认为它会在外观方面产生任何问题,即使您的textview和imageview总是分开的。不管怎样,你能不能把你的屏幕快照放在你当前正在显示的内容和你想要的内容上,这将很好地检测到实际的需求。我现在几乎不在办公室了……稍后再做。但是我开始考虑使用表格来代替RelayLayOut.Falm也有助于消除空间,减少视图计数,因此增加了性能。@ Fustigador,你也可以使用线性布局代替相对布局,并且可以给孩子提供权重,这样就可以解决这个问题。