日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

TextView 显示图像+文字的方法

發布時間:2024/4/15 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 TextView 显示图像+文字的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

TextView 通常的用法只是進行文字的顯示,但是仔細了解之后發現它可以顯示圖像信息。

本文將介紹此種用法:android:drawableLeft="@drawable/ic_launcher"及相應的編碼方法

利用TextView 進行圖像+文字的顯示時,有兩種方式: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>
該布局文件中垂直放入了兩個TextView以分別對對應這兩和種方式。

運行效果如下圖所示:


可以看到第一TextView中顯示了圖像加文字(設置顯示效果時注意布局文件中的其他屬性)。第二個TextView 還沒有通過編碼設置,所以顯示的只有文字。

下面通過編碼的方式達到和第一個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(){//獲取控件 并以編碼的方式設置其圖像myTextView2=(TextView)findViewById(R.id.myTextView2);Drawable imagmeDrawable=getResources().getDrawable(R.drawable.ic_launcher);//這一步一定要做,否則不會顯示圖像!!!!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);} }
運行之后的效果為:


可以看出能過編碼的方式也達到了相同的效果。

細心一點你會發現每一行最后都有“...”,如下圖所示:


實際上是因為每一行文字都沒有顯示完全,而顯示出來的。

這個跟TextView 的android:ellipsize屬性有關系,下面介紹一下該屬性。

TextView及其子類,當字符內容太長顯示不下時可以省略號代替未顯示的字符;省略號可以在顯示區域的起始,中間,結束位置,或者以跑馬燈的方式顯示文字(textview的狀態為被選中)

其實現只需在xml中對textviewellipsize屬性做相應的設置即可,或者在程序中可通過setEillpsize顯式設置。??

android:ellipsize="start"??????? 省略號在開頭??????? ???????? android:ellipsize="middle"?????? 省略號在中間??????? ???????? android:ellipsize="end"????????? 省略號在結尾??????? ???????? android:ellipsize="marquee"????? 跑馬燈顯示

注: EditText不支持marquee這種模式。Android:ellipsize屬性要配合android:MaxLines=“1”一起使用才能生效,因為如果不設置android:MaxLines=“1”的話,TextView中的文字默認多行顯示。



總結

以上是生活随笔為你收集整理的TextView 显示图像+文字的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。