簡體   English   中英

無法在Android 2.2上實現四舍五入的ListView

[英]Unable to achieve rounded ListView on Android 2.2

我有一個自定義的drawed rounded_corner.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
     <gradient android:startColor="#FFFFFF" android:endColor="#FFFFFF" 
            android:angle="90"/> 

    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" 
     android:topLeftRadius="7dp" android:topRightRadius="7dp"/> 
</shape> 

我通過代碼應用它:

listView.setBackgroundResource(R.drawable.rounded_corner);

但是我得到了錯誤:

5-09 20:39:17.434: ERROR/AndroidRuntime(1299): FATAL EXCEPTION: main
05-09 20:39:17.434: ERROR/AndroidRuntime(1299): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.stocktwits.activity/org.stocktwits.activity.WatchlistActivity}: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/rounded_corner.xml from drawable resource ID #0x7f02000d
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.view.View.performClick(View.java:2408)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.view.View$PerformClick.run(View.java:8816)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.os.Handler.handleCallback(Handler.java:587)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.os.Looper.loop(Looper.java:123)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.ActivityThread.main(ActivityThread.java:4627)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at java.lang.reflect.Method.invokeNative(Native Method)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at java.lang.reflect.Method.invoke(Method.java:521)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at dalvik.system.NativeStart.main(Native Method)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/rounded_corner.xml from drawable resource ID #0x7f02000d
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.content.res.Resources.loadDrawable(Resources.java:1697)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.content.res.Resources.getDrawable(Resources.java:581)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.view.View.setBackgroundResource(View.java:7393)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at org.stocktwits.activity.WatchlistActivity.setupUI(WatchlistActivity.java:184)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at org.stocktwits.activity.WatchlistActivity.onCreate(WatchlistActivity.java:127)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     ... 18 more
05-09 20:39:17.434: ERROR/AndroidRuntime(1299): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #4<gradient> tag requires 'angle' attribute to be a multiple of 45
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.graphics.drawable.GradientDrawable.inflate(GradientDrawable.java:678)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
05-09 20:39:17.434: ERROR/AndroidRuntime(1299):     at android.content.res.Resources.loadDrawable(Resources.java:1694)

我該如何診斷? 資源存在。

查看stacktrace的“ caused by”部分:二進制XML文件的第4行標簽要求'angle'屬性為45的倍數。

嘗試寫這個

代替
角落android:bottomRightRadius =“ 7dp” android:bottomLeftRadius =“ 7dp” android:topLeftRadius =“ 7dp” android:topRightRadius =“ 7dp”
將此.xml文件放在drawable文件夾中,然后嘗試使用
listView.setBackgroundDrawable(drawable rounded_corner.xml);

這是我在列表周圍添加圓形邊框的方式

<ListView android:layout_width="fill_parent" android:id="@id/android:list"
 android:layout_height="0dip" android:layout_weight="1.0"
 android:background="@drawable/app_user_list_drawable" android:fadeScrollbars="true"
 android:descendantFocusability="afterDescendants"  android:layout_margin="10dip"
 android:listSelector="@android:color/transparent" android:paddingLeft="10dip"
 android:paddingRight="10dip" android:divider="#7D7D7D" android:dividerHeight="1px"/>

app_user_list_drawable.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="1dip" android:right="0dip"
 android:bottom="1dip"/>
<stroke android:width="1dip" android:color="#7D7D7D"/>
</shape>

這在我的列表周圍創建了一個帶有圓邊的簡單白色邊框。 列表視圖中的填充是為了留出一點空間,或者在添加行時看起來會很尷尬。

暫無
暫無

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

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