簡體   English   中英

如何創建這樣的自定義小部件?

[英]How to create custom widget like this?

我想創建一個自定義小部件。

它看起來像這樣:
在此輸入圖像描述

那么,我應該擴展Button因為我想點擊它嗎?

我怎樣才能在上面放一張圖片? 我已經閱讀過onDraw()方法,但我不知道如何放置image

(感謝Andrew T.)

你可以把它變成一個compund對象:它是一個帶有drawable的TextView。

TextViews是可點擊的,因此您可以觸發單擊處理程序。

像這樣的東西:

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:drawableLeft="@drawable/ic_launcher"
    android:drawablePadding="8dp"
    android:text="Title\nA little description for this object"
    android:textSize="16sp"
    android:onClick="click_handler"
/>

然后只需添加到您的代碼:

public void click_handler(View v)
{
    // Do something in your click event
}

將所有內容放在LinearLayout或RelativeLayout(一個ImageView,兩個TextViews)上。 將click偵聽器添加到該布局。 稍后您可以使用ListViews中的布局。

你可以用它

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/imageView"
            ......
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ......
            android:layout_toRightOf="@+id/imageView"
            android:layout_alignTop="@+id/imageView" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ......
            android:layout_below="@+id/textView1"
            android:layout_alignLeft="@+id/textView1" />

    </RelativeLayout>

然后編寫一個類來處理它

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM