当我尝试使用时:
android.support.v4.widget.CircleImageView
<android.support.v4.widget.CircleImageView
android:id="@+id/picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_bg" />
这会使我的应用崩溃
如何支持新的Material Design小部件CircleImageView
有没有使用此新小部件的示例
Logcat
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vogella.android.recyclerview/com.vogella.android.recyclerview.MainActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v4.widget.CircleImageView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
at android.app.ActivityThread.access$700(ActivityThread.java:157)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5317)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class android.support.v4.widget.CircleImageView
at
最佳答案
CircleImageView
是支持库的私有(private)类,不能使用。但是,无需CircleImageView
即可轻松创建此效果。您只需要定义一个<shape />
可绘制对象,中间带有一个透明圆圈,如下所示:
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="1"
android:useLevel="false" >
<solid android:color="@android:color/transparent" />
<stroke
android:width="100dp"
android:color="#FFFFFFFF" />
</shape>
之后,只需将要显示在
ImageView
中的图像与可从上方以<shape />
绘制的LayerList
相结合即可,如下所示:<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/your_image" />
<item android:drawable="@drawable/circle" />
</layer-list>
如果要显示的图像是动态的,则可以通过编程方式创建
LayerList
!