简体   繁体   中英

Developing Material Design homescreen-widgets in Android

Ok, so I'm working on a very simple Android homescreen widget that fetches the latest Image from an imageboard and displays it in a imageview.

I'd like to have the option to Style this widget, to be in keeping with Googles Material design guidelines, however, I hit the following problem whilst developing and testing on my Nexus 5 running Lollipop 5.1 -

I can't seem to use a cardview in a widget. It gives me a class not found exception however I do not get such exceptions previewing the layout and my gradle has the right dependancies so I suspect it is more to do with widget compatibility?

So I did some further reading and on the Google API page about widgets it has a very specific list of views that widgets support, which did not, to my surprise include cardviews. So I've gone back to my plain old Imageview

I guess my question then sits with, how would I get this imageview to look like a material design card? EG, rounded corners, the subtle 3d effect, drop shadow, ETC. I do not wish to give the imageview a border, the edge of the image should extend to the edges and get clipped by the rounded corners.

I have tried by simply setting the style of the Imageview to be that of a cardview, but that seems to have done absolutely nada :)

Anyone has some ideas? Thank you!

您可能想看看Google I / O应用程序的来源: https : //github.com/google/iosched/blob/master/android/src/main/res/layout/widget.xml

This is from a long time ago, but it's very doable to make a simple card background in XML.

The way to do this is to create an cardview-esque .xml file in your drawables app resources folder.

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="@color/white" />
    <corners android:radius="4dp" />
</shape>

and set the background view of your Widget's XML layout as

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/widget_card_background"
    android:orientation="vertical">

    <.../>

</LinearLayout>

You are correct in that CardView is not an available View type for Android Widgets, but this should create the functionality you were looking for 2 years ago

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM