【问题标题】:TextView is not able to set ImageViewTextView 无法设置 ImageView
【发布时间】:2015-06-02 12:37:26
【问题描述】:

我是 Android 新手。我正在做一个布局。我想在imageview下面设置textview,但我做不到。当我自己将 textview 拖到图像下方时,RelativeLayout 变为 matchparent。请看我的代码。

<?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"
android:background="@drawable/sample" >

<ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/ic_launcher" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@id/imageView1"
    android:text="sbxcdgdbc dhwe hdejd djhe  dqe "
    android:textColor="#ffffff" />

</RelativeLayout> 

背景图片为500*350像素

【问题讨论】:

  • 因为TextView与ImageView不在同一层级。顺便说一句,你为什么要放置所有这些 LinearLayouts?它们不仅不利于性能,还会使您的设计混乱。顺便说一句,可以通过对 TextView 使用 compound drawable 来避免 ImageView,从而进一步提高性能。
  • @DerGolem 起初我在没有太多布局的情况下让它变得简单,但我遇到了错误,这就是我使用这些的原因。
  • @DerGolem 我已经更新了我的代码。即使现在它也不起作用
  • 现在你纠正了多余的+,是的。
  • @Strider fill_parent弃用。改用match_parent

标签: android xml


【解决方案1】:

尝试将这一行 android:layout_below="@+id/rr" 更改为 android:layout_below="@id/rr"。在第一个版本中,您说这是一个新 ID。在第二个版本中,您引用的是 ID,因此您不需要 +。

【讨论】:

  • 您在文本视图和上述线性布局之间的线性布局中也有那条线,其中包含图像视图。您应该尝试从中间线性布局中修复它(删除+)并给这个布局一个id。然后将下面的 id 设置为这个中间布局。所以你会有类似 rr 下面的第二个布局和下面的 textview 任何 id 你给中间线性布局
【解决方案2】:

如果你有这么简单的布局,你应该使用带有参数的 LinearLayout:orientation vertical。然后每个新视图都将在前面添加。稍后您可以使用边距参数来更改视图之间的空间。

【讨论】:

  • 当我使用 LinearLayout 时,我无法使用“CentralHorizo​​ntal & CentralVertical Params”
【解决方案3】:

您可以使用不同的方式来满足您的要求

  1. LinearLayout 具有垂直方向,其中的子级可以是 Textview 和 ImageView。

但问题在于它不是使用不同视图的最佳方式。

  1. 您可以使用具有一个 Textview 的子级相对/线性的任何父级布局,该 Textview 包含一个名为复合可绘制的标签并提供图像。

使用这种方法,您可以在 textview 的上方、下方、右侧或左侧拥有单个图像。我相信这是一个好方法。

【讨论】:

  • 能否请您告诉我在 textview 中使用的参数。
  • android:drawableEnd="@drawable/image_name"
【解决方案4】:

试试这个:

<?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"
                android:background="@drawable/sample">

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/ic_launcher"/>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/imageView1"
        android:text="sbxcdgdbc dhwe hdejd djhe  dqe "
        android:textColor="#ffffff"/>


</RelativeLayout>

【讨论】:

  • 我不希望它在顶部我只想要图像“CentralHorizo​​ntal & CentralVertical 以及此图像下方的文本视图
  • 你能发布一张图片来准确地展示你想要实现的目标吗?
  • 我只希望 ic_launcher 位于中心并在其下方显示文字。
【解决方案5】:

如果您的要求只是在 ImageView 下方显示 TextView,则应采用“垂直”方向的 LinearLayout,请参见以下代码:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:gravity="center|center"
android:background="@drawable/sample">
<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
   android:background="@drawable/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/linearLayout1"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center"
    android:text="sbxcdgdbc dhwe hdejd djhe  dqe "
    android:textColor="#ffffff" />
</LinearLayout>

根据您的评论编辑了答案。

【讨论】:

  • 我希望我的图像在中心(垂直和水平)和此图像下方的文本。
  • 确定在中心显示图像,在父 LinearLayout 中添加这两个属性, android:layout_height="match_parent" 并添加 android:gravity="center|center" ...根据您的编辑我的答案要求,请检查
  • 背景图像通过此代码延伸到整个屏幕(匹配父级)。图片为500*350像素
【解决方案6】:

编辑

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

<ImageView
    android:id="@+id/imageView0"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/sample" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:background="@mipmap/ic_launcher" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_centerHorizontal="true"
    android:text="sbxcdgdbc dhwe hdejd djhe  dqe "
    android:textColor="#ffffff"
    android:layout_below="@+id/imageView1"
    android:id="@+id/textView" />

</RelativeLayout>

【讨论】:

  • 我必须更改相对、图像或文本的位置。
  • @AmarbirSingh 在你的TextView
  • 我希望中心(水平和垂直)的图像不在顶部。查看我上传的图片。
  • @AmarbirSingh 然后将android:layout_gravity="center" 添加到RelativeLayout
  • @hrskrs 不,他需要将布局中的android:layout_height="wrap_content" 更改为:android:layout_height="fill_parent"
【解决方案7】:

这对我有用:

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/sample"
android:gravity="center"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_gravity="center_horizontal"
    android:background="@drawable/ic_launcher" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/imageView1"
    android:text="sbxcdgdbc dhwe hdejd djhe  dqe "
    android:textColor="#ffffff" />

 </LinearLayout>

【讨论】:

    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2016-11-01
    • 2019-01-21
    • 1970-01-01
    • 2020-03-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多