2014-03-02 28 views
0

所以,我遇到了一個問題。我有一個ListView看起來像這樣:如何在每個ListView項目中四捨五入...?

enter image description here

嗯,我一直在尋找一個很長時間才能找到答案,我的問題。所以我的問題是,我無法在每個項目中四捨五入,只能在所有列表視圖中四捨五入。這是我的列表視圖樣式代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/green"> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:divider="@color/red" 
     android:dividerHeight="8dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginLeft="15dp" 
     android:layout_marginTop="10dp" 
     android:layout_marginBottom="5dp" 
     android:background="@drawable/listviewstyle"> 
    </ListView> 

</LinearLayout> 

有人可以幫我找出如何將每個列表視圖項目角舍入?

回答

2

您應該將style應用於列表的每個元素,而不是將其應用於整個列表。

1

設置

android:background="@drawable/listviewstyle" 

到列表中的每一項

1

創建shape-drawable並給予radius任何價值,你需要

custom_list_item.xml(在drawable文件夾)。我使用這些屬性自定義Toast但你可以改變你的需求

<?xml version="1.0" encoding="utf-8"?> 
<!-- res/drawable/rounded_edittext.xml --> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" android:padding="10dp"> 
<solid android:color="@drawable/white"/> 
    <corners 
     android:bottomRightRadius="45dp" 
     android:bottomLeftRadius="45dp" 
     android:topLeftRadius="45dp" 
     android:topRightRadius="45dp"/> 
    <stroke android:color="@drawable/orange" 
     android:width="3px"/> 
</shape> 

然後您爲ListViewlayout。您可以將此shape-drawable列表項的背景或把它放在一個風格styles.xml和風格應用到您的ListView項目layout

styles.xml創建樣式

<style name="CustomListItem" parent="android:Widget.TextView"> 
    <item name="android:background">@drawable/custom_list_item</item> 
    <item name="android:textColor">@drawable/black</item> 
</style> 

然後在您的自定義列表項layout中添加

style="@style/CustomListItem" 
+0

注意列表的底部... – pskink

+0

@pskink是的,因爲它是不適用於每個項目 – codeMagic

+0

不,他希望他的ListView被一個圓形矩形「剪裁」,這就是爲什麼你看到最後一個可見的項目圓,但不是因爲它本身圓,而是圓形列表視圖剪輯它 – pskink

1

您目前正在將您的樣式應用於整個列表,您確實需要將其設置在適配器上,而不是整個列表中。

在你的榜樣,你需要從你的ListView XML刪除此行:

android:background="@drawable/listviewstyle" 

然後在適配器您使用的列表元素,把這個有。 (提示:這是你用於描述元素1和「子元素1」的佈局),