Android 實現文字與圖片的混排

來源:互聯網
上載者:User

標籤:指定   int   java代碼   .text   水平   不顯示   tracking   size   style   

在我們的項目中,常常會碰到圖片與文字混排的問題。解決這類問題的方法有非常多,本文給出的方法不是唯一的。僅僅有依據實際情境才幹找到更適合的方法。

本文主要通過xml布局來實現圖片與文字的混排(水平排列)。


1.利用TextView實現圖片與文字混排,

android:drawableBottom在text的下方輸出一個drawable。片。

假設指定一個顏色的話會把text的背景設為該顏色。而且同一時候和background使用時覆蓋後者。
android:drawableLeft在text的左邊輸出一個drawable,片。
android:drawablePadding設定text與drawable(圖片)的間隔,

與drawableLeft、 drawableRight、drawableTop、drawableBottom一起使用,可設定為負數。單獨使用沒有效果。


android:drawableRight在text的右邊輸出一個drawable。


android:drawableTop在text的正上方輸出一個drawable。


<span style="font-size:18px;">        <TextView            android:id="@+id/my_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="線上"            android:textColor="#85898f"            android:layout_marginTop="5dp"            android:drawablePadding="5dp"            android:drawableLeft="@drawable/user_online"/></span>

當中, android:drawablePaddingh非常好的攻克了圖片與文字的間距問題。

2.TextView動態設定圖片

Drawable drawable= context.getResources().getDrawable(R.drawable.text_img);// 調用setCompoundDrawables時。必須調用Drawable.setBounds()方法,否則圖片不顯示drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());textView.setCompoundDrawables(drawable, null, null, null); //設定左表徵圖

3.利用RelativeLayout(LinearLayout) 加入 TextView 和 ImageView(ButtonView)來實現

<span style="font-size:18px;">   <RelativeLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content" >        <ImageView            android:id="@+id/my_iv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:background="@drawable/user_online"            android:layout_alignParentLeft="true"            android:layout_centerVertical="true"            android:layout_marginLeft="5dp"            />                <TextView            android:id="@+id/my_tv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"             android:layout_toRightOf="@id/my_iv"            android:layout_centerVertical="true"            android:layout_marginLeft="5dp"            />    </RelativeLayout></span>


事實上也能夠通過java代碼來實現圖片和文字的混排。



   

Android 實現文字與圖片的混排

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.