[英]Android ListView Divider
我有這個代碼:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cashItemsList"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"></ListView>
其中@drawable/list_divider
是:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#8F8F8F"
android:dashWidth="1dp"
android:dashGap="1dp" />
</shape>
但我看不到任何分隔線。
伙計們,這就是為什么您應該使用 1px 而不是 1dp 或 1dip 的原因:如果您指定 1dp 或 1dip,Android 會將其縮小。 在 120dpi 的設備上,它變成了像 0.75px 轉換的東西,它四舍五入為 0。在某些設備上,它轉換為 2-3 像素,它通常看起來很難看或馬虎
對於分隔線,如果您想要一個 1 像素的分隔線,則 1px 是正確的高度,並且是“一切都應該傾斜”規則的例外之一。 它在所有屏幕上都是 1 像素。 另外,1px 通常在 hdpi 及以上屏幕上看起來更好
“不再是 2012 年了”編輯:您可能必須從某個屏幕密度開始切換到 dp/dip
這是一種解決方法,但對我有用:
創建 res/drawable/divider.xml 如下:
<?xml version="1.0" encoding="UTF-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="#ffcdcdcd" android:endColor="#ffcdcdcd" android:angle="270.0" />
</shape>
在styles.xml 的listview 項中,我添加了以下幾行:
<item name="android:divider">@drawable/divider</item>
<item name="android:dividerHeight">1px</item>
關鍵部分是包含這個 1px 設置。 當然,drawable 使用漸變(1px),這不是最佳解決方案。 我嘗試使用中風,但沒有讓它起作用。 (您似乎沒有使用樣式,因此只需為 ListView 添加 android:dividerHeight="1px" 屬性。
添加android:dividerHeight="1px"
它將起作用:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cashItemsList"
android:cacheColorHint="#00000000"
android:divider="@drawable/list_divider"
android:dividerHeight="1px">
</ListView>
您遇到的問題源於您缺少所需的 android:dividerHeight,以及您嘗試在可繪制對象中指定線寬的事實,而某些情況下您無法使用分隔線奇怪的原因。 基本上為了讓您的示例工作,您可以執行以下操作:
將您的可繪制對象創建為矩形或線條,要么有效,您只是無法嘗試在其上設置任何尺寸,因此:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line">
<stroke android:color="#8F8F8F" android:dashWidth="1dp" android:dashGap="1dp" />
</shape>
或:
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#8F8F8F"/>
</shape>
然后創建一個自定義樣式(只是一個偏好,但我喜歡能夠重復使用的東西)
<style name="dividedListStyle" parent="@android:style/Widget.ListView">
<item name="android:cacheColorHint">@android:color/transparent</item>
<item name="android:divider">@drawable/list_divider</item>
<item name="android:dividerHeight">1dp</item>
</style>
最后使用自定義樣式聲明您的列表視圖:
<ListView
style="@style/dividedListStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cashItemsList">
</ListView>
我假設你知道如何使用這些片段,如果不知道的話。 基本上你的問題的答案是你不能在 drawable 中設置分隔線的厚度,你必須在那里未定義寬度並使用 android:dividerHeight 來設置它。
從文檔:
public void setDivider(Drawable divider) on ListView
/**
* Sets the drawable that will be drawn between each item in the list. If the drawable does
* not have an intrinsic height, you should also call {@link #setDividerHeight(int)}
*
* @param divider The drawable to use.
*/
看起來必須調用setDividerHeight()
才能讓分隔線在沒有固有高度的情況下顯示
你的@drawable/list_divide
應該是這樣的:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:height="1dp"
android:color="#8F8F8F"
android:dashWidth="1dp"
android:dashGap="1dp" />
</shape>
在您的版本中,您提供了android:width="1dp"
,只需將其更改為android:height="1dp"
就可以了!
從文檔:
文件位置:
res/drawable/filename.xml
文件名用作資源 ID 。
基本上,您需要在res/drawable/
放置一個名為list_divider.xml
的文件,以便您可以將其作為R.drawable.list_divider
訪問; 如果您可以通過這種方式訪問它,那么您可以在ListView
的 XML 中使用android:divider="@drawable/list_divider"
。
有些人可能會遇到一條實線。 我通過將android:layerType="software"
到引用可繪制對象的視圖來解決這個問題。
我有同樣的問題。 然而,使視圖 1px 在我原來的 Nexus 7 上似乎不起作用。我注意到屏幕密度是 213,這小於 xhdpi 中使用的 240。 所以它認為設備是 mdpi 密度。
我的解決方案是讓dimens
文件夾有一個dividerHeight
參數。 我將它設置為2dp
的values-mdpi
文件夾,但是1dp
的values-hdpi
等文件夾。
您在分隔線 xml 布局中的分隔線末尾忘記了“r”
您調用布局 @drawable/list_divider 但您的分隔線 xml 被命名為“list_divide”
android 文檔警告有關由於舍入錯誤而消失的事情......也許嘗試使用 dp 而不是 px,也可能首先嘗試 > 1 以查看它是否是舍入問題。
見http://developer.android.com/guide/practices/screens_support.html#testing
對於“具有 1 個像素高度/寬度的圖像”部分
設置android:dividerHeight="1dp"
<ListView
android:id="@+id/myphnview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@drawable/dividerheight"
android:background="#E9EAEC"
android:clickable="true"
android:divider="@color/white"
android:dividerHeight="1dp"
android:headerDividersEnabled="true" >
</ListView>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.