簡體   English   中英

如何設置列表視圖中所有項目的 alpha?

[英]How do I set the alpha of all items in a listview?

因為我正在為 API 級別 7 和更高級別開發setAlpha(float)對我來說不可用。 因此,我實現了一種遍歷給定ViewGroup所有子元素的方法,並嘗試找到一種設置 alpha 的方法,以便元素幾乎透明。 不幸的是,我無法想出一種方法來使ListView及其項目透明。 我該如何進行?

我的方法:

public void enableDisableViewGroup(ViewGroup viewGroup, boolean enabled) {
    int childCount = viewGroup.getChildCount();
    for (int i = 0; i < childCount; i++) {
        View view = viewGroup.getChildAt(i);

        view.setEnabled(enabled);
        if (!enabled) {
            if (view instanceof TextView) {
                int curColor = ((TextView) view).getCurrentTextColor();
                int myColor = Color.argb(ALPHA_NUM, Color.red(curColor),
                        Color.green(curColor),
                        Color.blue(curColor));

                ((TextView) view).setTextColor(myColor);
            } else if (view instanceof ImageView) {
                ((ImageView) view).setAlpha(ALPHA_NUM);
            } else if (view instanceof ListView) {
                // How do I set the text color of the subitems in this listview?
            } else {
                try {
                    Paint currentBackgroundPaint =
                            ((PaintDrawable) view.getBackground()).getPaint();
                    int curColor = currentBackgroundPaint.getColor();
                    int myColor = Color.argb(ALPHA_NUM, Color.red(curColor),
                            Color.green(curColor), Color.blue(curColor));
                    view.setBackgroundColor(myColor);
                } catch (NullPointerException e) {
                    Log.d("viewNotFound", "View " + view.getId() + " not found..");
                    e.getStackTrace();
                }
            }
            if (view instanceof ViewGroup) {
                enableDisableViewGroup((ViewGroup) view, enabled);
            }
        }
    }
}

是否可以直接在 XML 中為ListView行設置此項?

例如,如果您的布局是LinearLayout

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent">

    <!-- stuff -->
</LinearLayout>

我不確定你的設置,但如果你真的希望你的ListView是透明的,你可以像這樣以編程方式設置它。

ListView listView = (ListView) view;
listView.setBackgroundColor(android.R.color.transparent);

其中默認顏色android.R.color.transparent等於:

<!-- Fully transparent, equivalent to 0x00000000 -->
<color name="transparent">#00000000</color>

在這種情況下,無論如何您都需要將ListView行設置為在 XML 中透明。

如果您想控制ListView行內View的透明度,最好的辦法是創建一個自定義ArrayAdapter並在那里處理。

暫無
暫無

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

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