TextView 显示图像+文字的方法
TextView 通常的用法只是進(jìn)行文字的顯示,但是仔細(xì)了解之后發(fā)現(xiàn)它可以顯示圖像信息。
本文將介紹此種用法:android:drawableLeft="@drawable/ic_launcher"及相應(yīng)的編碼方法
利用TextView 進(jìn)行圖像+文字的顯示時(shí),有兩種方式:xml文件方式和編碼方式
代碼如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"android:padding="6dip"><TextView android:id="@+id/myTextView1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="This is a TextView with an image in the left.This is a TextView with an image in the left.This is a TextView with an image in the left."android:ellipsize="end"android:maxLines="1"android:layout_marginLeft="20dp"android:drawablePadding="50dp"android:drawableLeft="@drawable/ic_launcher"/><TextView android:id="@+id/myTextView2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="This is a TextView with an image in the left.This is a TextView with an image in the left.This is a TextView with an image in the left."android:ellipsize="end"android:layout_marginLeft="20dp"android:drawablePadding="50dp"/></LinearLayout>該布局文件中垂直放入了兩個(gè)TextView以分別對(duì)對(duì)應(yīng)這兩和種方式。
運(yùn)行效果如下圖所示:
可以看到第一TextView中顯示了圖像加文字(設(shè)置顯示效果時(shí)注意布局文件中的其他屬性)。第二個(gè)TextView 還沒(méi)有通過(guò)編碼設(shè)置,所以顯示的只有文字。
下面通過(guò)編碼的方式達(dá)到和第一個(gè)TextView 中相同的效果:
package com.demo.demotest;import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView;public class MainActivity extends ActionBarActivity {private TextView myTextView2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}//初始化控件private void init(){//獲取控件 并以編碼的方式設(shè)置其圖像myTextView2=(TextView)findViewById(R.id.myTextView2);Drawable imagmeDrawable=getResources().getDrawable(R.drawable.ic_launcher);//這一步一定要做,否則不會(huì)顯示圖像!!!!imagmeDrawable.setBounds(0, 0, imagmeDrawable .getMinimumWidth(), imagmeDrawable.getMinimumHeight());myTextView2.setCompoundDrawables(imagmeDrawable, null,null, null);myTextView2.setMaxLines(1);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);} }運(yùn)行之后的效果為:
可以看出能過(guò)編碼的方式也達(dá)到了相同的效果。
細(xì)心一點(diǎn)你會(huì)發(fā)現(xiàn)每一行最后都有“...”,如下圖所示:
實(shí)際上是因?yàn)槊恳恍形淖侄紱](méi)有顯示完全,而顯示出來(lái)的。
這個(gè)跟TextView 的android:ellipsize屬性有關(guān)系,下面介紹一下該屬性。
TextView及其子類,當(dāng)字符內(nèi)容太長(zhǎng)顯示不下時(shí)可以省略號(hào)代替未顯示的字符;省略號(hào)可以在顯示區(qū)域的起始,中間,結(jié)束位置,或者以跑馬燈的方式顯示文字(textview的狀態(tài)為被選中)。
其實(shí)現(xiàn)只需在xml中對(duì)textview的ellipsize屬性做相應(yīng)的設(shè)置即可,或者在程序中可通過(guò)setEillpsize顯式設(shè)置。??
android:ellipsize="start"??????? 省略號(hào)在開(kāi)頭??????? ???????? android:ellipsize="middle"?????? 省略號(hào)在中間??????? ???????? android:ellipsize="end"????????? 省略號(hào)在結(jié)尾??????? ???????? android:ellipsize="marquee"????? 跑馬燈顯示 注: EditText不支持marquee這種模式。Android:ellipsize屬性要配合android:MaxLines=“1”一起使用才能生效,因?yàn)槿绻辉O(shè)置android:MaxLines=“1”的話,TextView中的文字默認(rèn)多行顯示。
總結(jié)
以上是生活随笔為你收集整理的TextView 显示图像+文字的方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 友盟分享快速集成--学习记录
- 下一篇: Android TextView中文字通