Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 布局技巧:TextView 與 ImageView 的靈活組合

編輯:關於 Android 程式設計

在 Android 介面開發中,若想讓文字旁搭配小圖示,很多人直覺會想到用 RelativeLayout 去拼接 ImageView 與 TextView。但其實 TextView 本身就提供了更方便的屬性,可以直接在文字的四個方位放置圖元,省去額外佈局的麻煩。

以下是一個簡單的範例:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:drawableLeft="@drawable/icon"
        android:text="@string/app_name"
        />

</LinearLayout>

看到 android:drawableLeft 這個屬性,相信大家都明白了!TextView 提供了四個方位的圖元屬性:

android:drawableLeft=""      // 文字左側
android:drawableRight=""     // 文字右側
android:drawableTop=""       // 文字上方
android:drawableBottom=""    // 文字下方

此外,還有 android:drawablePadding 可以用來設定圖元與文字之間的間距,非常實用。以往需要靠 RelativeLayout 層層堆疊才能達到的效果,如今一個屬性就能搞定,既簡潔又直觀。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved