簡體   English   中英

Android主屏幕小部件(圖標,標簽 - 樣式)

[英]Android Home Screen Widget (icon, label - style)

我正在嘗試創建一個可放置在android主屏幕上的圖標/小部件(1個單元格x 1單元格)。 該小部件的外觀和行為與android中的其他標准快捷方式完全相同。 它將有一個圖標,在該標簽下,它可以通過軌跡球(高亮顯示)進行選擇,它將在選擇/點擊時突出顯示。

如何創建此主屏幕小部件?

我是否必須使用code / xml自己創建窗口小部件,或者是否有一些標准的xml,樣式,主題和代碼可用於確保窗口小部件與其他主屏幕窗口小部件具有相同的樣式/主題?

我目前有以下內容

RES /抽拉/ corners.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Corners">
    <stroke android:width="4dp" android:color="#CC222222"  />
    <padding android:left="4dp" android:top="1dp" android:right="4dp" android:bottom="1dp" />
    <corners android:radius="4dp" />
</shape>

RES /布局/ widget.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/Widget"
    android:layout_width="72dip"
    android:layout_height="72dip"
    android:orientation="vertical"
    android:focusable="true"
    android:gravity="center_horizontal"
    style="@android:style/Widget"   
    >

    <ImageView
        android:id="@+id/WidgetIcon"
        android:src="@drawable/icon"
        android:layout_width="fill_parent" 
        android:layout_height="50dip"
        android:paddingTop="3dip"
        android:gravity="center"
        />

    <TextView
        android:id="@+id/WidgetLabel"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:text="@string/app_name"
        android:textSize="15dip"
        android:background="@drawable/corners"
        />

</LinearLayout>

生成的窗口小部件看起來有些接近,但它不可選擇,單擊時不會突出顯示,並且標簽不完全位於正確的位置或正確的樣式。

任何想法,如果有正確的方法,或者我應該繼續努力,直到我更接近?

“執行此操作的正確方法”是創建快捷方式,而不是嘗試使用應用小部件來模仿它。 核心Android團隊(特別是Romain Guy)在[android-developers]討論列表中反復指出過, 例如

小部件應該看起來像小部件,而不是快捷方式。 主要原因是絕對不能保證快捷方式的樣子。 其他設備(特別是具有自定義系統UI的設備,如MOTOBLUR或HTC Sense)可能具有不同的外觀和感覺。 或者在Android的下一次更新中,我們可能會更改快捷方式的顯示方式。

通過引用系統屬性android:attr/selectableItemBackground來使您的項目與系統外觀一致並不難。 例如,如果您想在窗口小部件中使用適當的突出顯示等“可選擇”,那么就這樣做了

<ImageView
    ...
    android:background="?android:attr/selectableItemBackground"
    ...
    />

android_sdk_path/platforms/android-x中有很多。 快樂挖!

編輯:我后來發現這個簡單的屬性不存在於SDK <v11中。 所以我現在知道的最好的方法是下載appwidget模板包並使用它。

暫無
暫無

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

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