簡體   English   中英

如何在沒有 ImageView 的 TextView 中間的 TextView 上添加圖像

[英]How to add image on TextView on the middle of the TextView without ImageView

我想在有關它的信息下方添加來自公司位置的圖像,但我也想在圖像下方添加文本。 我嘗試了在互聯網上找到的代碼,但它的作用是圖像在左側,文本在右側

這是代碼:

    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);

我試圖插入最后的圖像代碼,在中間,但沒有用,甚至不顯示圖像。

先感謝您。

ImageSpan ,您需要使用ImageSpan 我已經用 Kotlin 編寫了代碼,但是推斷 Java 應該相當簡單。 就像是:
科特林

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

爪哇:

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);

您應該在您的 xml 中執行此操作。 使用重力屬性,您可以選擇開始、中心或結束的圖像位置。

在您的 .xml 文件中使用 ConstraintLayout 以便您可以將圖像和文本准確放置在您想要的位置。

我可能是錯的,但看起來你要求的只是一列

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>

使用現有布局文件的副本,我可以使下面的內容更具體一些。 您可以應用邊距和填充來將內容准確放置在您想要的位置。

可以通過xml設置:

   <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:drawablePadding="8dp"
        android:drawableTop="@drawable/YOUR_IMAGE"/>
 <?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>

嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM