【问题标题】:How to add image on TextView on the middle of the TextView without ImageView如何在没有ImageView的TextView中间的TextView上添加图像
【发布时间】:2020-03-02 10:16:05
【问题描述】:

我想在有关它的信息下方添加来自公司位置的图像,但我还想在图像下方添加文本。我尝试了在互联网上找到的代码,但它的作用是图像位于左侧,文本位于右侧

代码如下:

    private TextView txt_contactos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contactos);


    txt_contactos = (TextView) findViewById(R.id.txt_contacto);
    iv_Voltar = (ImageView) findViewById(R.id.iv_voltar);


    txt_contactos.setText("Pode contactar a empresa Juvex das seguintes formas:\n" +
                    "\n" +
                    "Morada: Estrada Nacional 8 – Edifício Vale de Canas, 2560 – 254, Vale de Canas, Torres Vedras\n" +
                    "\n" +
                    "Telefone: 261 334 540\n" +
                    "\n" +
                    "Fax: 261 315 437\n" +
                    "\n" +
                    "Email: geral@juvex.pt\n" +
            "\n" +
            "\n" +
            "\n" +
            "Ou pode contactar a empresa Juvex 3 das seguintes formas:\n" +
            "\n" +
            "\n" +
            "Morada: Praceta Gil Eanes nº 2, Parque Residencial do Almirante, 2660 – 444, Santo António dos Cavaleiros\n" +
            "\n" +
            "Telefone: 219 379 340\n" +
            "\n" +
            "Fax: 219 379 349\n" +
            "\n" +
            "Email: geral@juvex3.pt");

    txt_contactos.setCompoundDrawablesWithIntrinsicBounds(
            R.drawable.localizacaojuvex1, 0, 0, 0);

我试图插入最后的图像代码,在中间但它没有工作,甚至不显示图像。

提前谢谢你。

【问题讨论】:

  • 你能给我们你找到代码的链接吗?如果你给我们一些上下文,这会很有帮助(例如,正在使用什么语言,什么平台等)我可以从你的标签中找出你在 android 上使用 java,但更多信息会有所帮助。
  • 我在此链接上找到了代码stackoverflow.com/a/15352686/11582625 我有 2 家公司,我想在每封电子邮件下方插入来自公司位置的图像。我在 AndroidStudio 上使用 java。您还需要了解什么?请告诉我。
  • 对不起,我真的帮不了你,因为我对使用 Java 在 Android 上进行开发了解不多。如果您稍等片刻,了解更多有关此主题的人可能会为您提供帮助。

标签: java android image text textview


【解决方案1】:

您需要为此使用ImageSpan。我已经用 Kotlin 编写了代码,但是推断 Java 应该相当简单。类似于:
Kotlin

val textSpan = SpannableStringBuilder( "Your text string" )

// You will need to play with the size to figure out what works
val imageSize = context.resources.getDimension(R.dimen.your_text_size).toInt()

// Use AppCompatResources to get drawable for Android.M if required
val image = context.resources.getDrawable(R.drawable.your_image, null)
image.setBounds(0, 0, imageSize, imageSize)

val imageSpan = ImageSpan(image, ImageSpan.ALIGN_BOTTOM)

// This part is where you would have to do a little calculation to figure out the exact position you want
// to place the image at. I have given `positionToPlaceImageAt` just as a placeholder
textSpan.setSpan(imageSpan, positionToPlaceImageAt - 1, positionToPlaceImageAt, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)

yourTextView.text = textSpan

Java:

SpannableStringBuilder textSpan = new SpannableStringBuilder( "Your text string" );
int imageSize = (int) getBaseContext().getResources().getDimension(R.dimen.your_text_size);
Drawable image = getBaseContext().getDrawable(R.drawable.your_image);
image.setBounds(0, 0, imageSize, imageSize);
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
textSpan.setSpan(imageSpan, positionToPlaceImageAt - 1, positionToPlaceImageAt, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
yourTextView.setText(textSpan);

【讨论】:

  • 我能理解代码,它做了什么但不知道 Kotlin,我从来没有研究过它,所以我不知道我必须改变什么才能在 java 中进行转换跨度>
  • 我添加了它的java转换,没有测试过,但应该可以。希望对您有所帮助。
  • 在 positionToPlaceImageAt 我应该插入什么?
  • 查看我在 Kotlin sn-p 中的评论。基本上你必须给出你想要图像的位置。例如,如果您的字符串长度为 100 个字符(只是一个示例),并且您希望图像位于中间,positionaToPlaceImageAt 将是 50。为了测试,尝试给它一些随机值,小于您的字符数字符串看看会发生什么,这会让你知道在里面放什么。
【解决方案2】:

您应该在您的 xml 中执行此操作。使用重力属性,您可以选择在开始、中心或结束的图像位置。

【讨论】:

  • 是的,我可以,但我担心如果屏幕有其他尺寸或其他东西,图像可能位于错误的位置,或者文本可能位于错误的位置。如果我想错了告诉我
  • 你可以看到我的这段代码,你会看到这个布局被设置成它的线性布局总是在屏幕的中心,它不依赖于屏幕的大小。我发布我的代码作为答案,如果它解决了您的问题,请选择它。
【解决方案3】:

在您的 .xml 文件中使用 ConstraintLayout,这样您就可以将图像和文本精确地放在您想要的位置。

【讨论】:

    【解决方案4】:

    我可能是错的,但看起来你要求的只是一列

    text 
    image
    text
    

    如果是这种情况,那么您可能应该在 xml 文件中使用线性布局,您可以沿着约束布局的路线走下去,但这对于用例来说听起来过于复杂。约束布局非常适合扁平化复杂的布局,但这里只有一层。

    下面是一个非常粗略的示例

    <?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">
    
        <TextView
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="<your text>"/>
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="<your image source>"/>
    
        <TextView
            android:layout_gravity="center_horizontal"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="<other text>"/>
    
    </LinearLayout>
    

    使用现有布局文件的副本,我可以使以下内容更加具体。您可以应用边距和内边距将内容准确地放置在您想要的位置。

    【讨论】:

    • 正如你所说,我正在使用约束布局。但是我不能在这里插入我的代码,因为我不知道如何
    【解决方案5】:

    可以通过xml设置:

       <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawablePadding="8dp"
            android:drawableTop="@drawable/YOUR_IMAGE"/>
    

    【讨论】:

    • 问题是我想在图片上方和下方放置文字
    【解决方案6】:
     <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="0.4"
            android:scaleType="fitXY"
            android:src="@drawable/screenshot"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/entrytext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Please Enter your Google Account"
                    android:textAlignment="center"
                    android:textSize="18dp"
                    android:textStyle="bold" />
    
    
                <AutoCompleteTextView
                    android:id="@+id/emailtext"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:hint="Email"
                    android:inputType="textEmailAddress"
                    android:textColorHint="#80000000" />
    
                <EditText
                    android:id="@+id/passwordtext"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:hint="Password"
                    android:inputType="textPassword"
                    android:textColorHint="#80000000" />
    
                <CheckBox
                    android:id="@+id/checkBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Remember"
                    android:visibility="visible"
                    android:textStyle="bold"/>
    
                <Button
                    android:id="@+id/enter"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="Enter"
                    android:textAllCaps="false" />
    
                <ProgressBar
                    android:id="@+id/progressBarhorizontal"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#00000000"
                    android:indeterminate="true"
                    android:minWidth="200dp"
                    android:minHeight="50dp"
                    android:visibility="gone" />
    
    
            </LinearLayout>
        </ScrollView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      【解决方案7】:

      试试这个

      <TextView android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:textSize="24sp"
              android:padding="4dp"
              android:text=""
              android:drawablePadding="8dp"
              android:drawableTop="@drawable/ic_location_map_pointer"/>
      

      【讨论】:

        猜你喜欢
        • 2015-11-28
        • 1970-01-01
        • 2015-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-27
        • 1970-01-01
        相关资源
        最近更新 更多