New UI-帶圖片(drawableXxx)的TextView,

來源:互聯網
上載者:User

New UI-帶圖片(drawableXxx)的TextView,

New UI-帶圖片(drawableXxx)的TextView

 ——轉載請註明出處:coder-pig,歡迎轉載,請勿用於商業用途!


小豬Android開發交流群已建立,歡迎大家加入,無論是新手,菜鳥,大神都可以,小豬一個人的

力量畢竟是有限的,寫出來的東西肯定會有很多紕漏不足,歡迎大家指出,集思廣益,讓小豬的博文

更加的詳盡,幫到更多的人,O(∩_∩)O謝謝!

小豬Android開發交流群:小豬Android開發交流群群號:421858269

新Android UI執行個體大全目錄:http://blog.csdn.net/coder_pig/article/details/42145907



本節引言:

在實際開發的時候我們可能會遇到這種情況:


一個小圖片+一個文字,然後放在一個LinearLayout中,這樣需要4個LinearLayout,很明顯

很浪費,這個時候我們就可以用到一個drawableTop來設定一個帶圖片的TextView!



本節本文:

1.基本用法:

設定圖片的核心其實就是:drawableXxx;可以設定四個方向的圖片:

drawableTop(上),drawableButtom(下),drawableLeft(左),drawableRight(右)

另外,你也可以使用drawablePadding設定圖片與文字間的間距:drawablePadding


這裡示範下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.jay.example.test.MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true"        android:drawableTop="@drawable/show1"        android:drawableLeft="@drawable/show1"        android:drawableRight="@drawable/show1"        android:drawableBottom="@drawable/show1"        android:drawablePadding="10dp"        android:text="張全蛋" /></RelativeLayout>

運行:




另外,還有一個問得比較多的一個問題就是:

2.如何來設定這個drawable的大小?

是不能直接在xml進行設定的,這就需要我們在Java代碼中來進行修改了:

Java程式碼範例如下:

package com.jay.example.test;import android.app.Activity;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.widget.TextView;public class MainActivity extends Activity {private TextView txtZQD;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);txtZQD = (TextView) findViewById(R.id.txtZQD);Drawable[] drawable = txtZQD.getCompoundDrawables();// 數組下表0~3,依次是:左上右下drawable[1].setBounds(100, 0, 200, 200);txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],drawable[3]);}}

看下效果先:



程式碼分析:

①Drawable[] drawable = txtZQD.getCompoundDrawables( );

獲得四個不同方向上的圖片資源,數組元素依次是:左上右下的圖片

②drawable[1].setBounds(100, 0, 200, 200);
接著獲得資源後,可以調用setBounds設定左上右下座標點,比如這裡設定了代表的是:

長是:從離文字最左邊開始100dp處到200dp處

寬是:從文字上方0dp處往上延伸200dp!

③txtZQD.setCompoundDrawables(drawable[0], drawable[1], drawable[2],
drawable[3]);

為TextView重新設定drawable數組!沒有圖片可以用null代替哦!




再接著就是

3..9圖片的問題

在Java代碼中為TextView設定Drawable的代碼如下:

Drawable top = getResources().getDrawable(R.drawable.ic); top.setBounds(0, 20, top.getMinimumWidth(),top.getMinimumHeight() + 20); view.setCompoundDrawables(top, null,null, null); 


最後再分享一個

4.將png轉換為bitmap的方法吧:

//Drawable轉換為Bitmap的方法public static Bitmap drawableToBitmap(Drawable drawable) {                   Bitmap bitmap = Bitmap                        .createBitmap(                                        drawable.getIntrinsicWidth(),                                        drawable.getIntrinsicHeight(),                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888                                                        : Bitmap.Config.RGB_565);        Canvas canvas = new Canvas(bitmap);        //canvas.setBitmap(bitmap);        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());        drawable.draw(canvas);        return bitmap;}




好了,關於TextView設定不同方向的Drawable就介紹到這裡~






聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.