2013-03-11 66 views
24

我想(徒勞)將邊距添加到我的ListView項目。我已經嘗試在下面的RelativeLayout中添加邊距值,但無論我做了什麼,我似乎都得到了每個項目之間1px的行。保證金在Android的ListView項目

我真正想要的是在每件商品上都有圓角,1px黑色邊框和3-5px的邊距,左上角和右下角,但現在我只能在每件商品的邊緣解決問題: - )

我該如何實現自己的目標?只是現在的保證金... ;-)

這是我有:

更新:我已經更新了以下主要去除佈局和片段佈局的XML。我也更新了ListView項目佈局,使其更接近我想要的但仍不完美的東西。截圖添加以及

列表視圖項佈局XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="@dimen/matchMargin" 
android:paddingRight="@dimen/matchMargin" 
android:paddingTop="@dimen/matchMargin" > 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#cfcfcfcf" > 

    <include 
     android:id="@+id/matchKampstart" 
     layout="@layout/kampstart_layout" /> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@id/matchKampstart" 
     android:layout_marginTop="@dimen/belowKampstartMargin" > 

     <ImageView 
      android:id="@+id/tournamentImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/tournamentImageViewContentDescription" 
      android:gravity="left" 
      android:src="@drawable/sofabold_launcher" /> 

     <ImageView 
      android:id="@+id/homeTeamImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/homeTeamImageViewContentDescription" 
      android:src="@drawable/sofabold_launcher" /> 

     <TextView 
      android:id="@+id/homeTeam" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:text="@string/home" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 

     <TextView 
      android:id="@+id/dash" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:gravity="center" 
      android:text="@string/dash" 
      android:textSize="12sp" 
      android:textStyle="bold" /> 

     <ImageView 
      android:id="@+id/awayTeamImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:layout_marginRight="2dp" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/awayTeamImageViewContentDescription" 
      android:src="@drawable/sofabold_launcher" /> 

     <TextView 
      android:id="@+id/awayTeam" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_vertical" 
      android:gravity="center" 
      android:text="@string/away" 
      android:textSize="14sp" 
      android:textStyle="bold" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/matchKampstart" 
     android:layout_marginTop="@dimen/belowKampstartMargin" > 

     <ImageView 
      android:id="@+id/tvChannelImageView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="false" 
      android:contentDescription="@string/tvChannelImageViewContentDescription" 
      android:gravity="right" 
      android:src="@drawable/sofabold_launcher" /> 
    </LinearLayout> 

</RelativeLayout> 

</RelativeLayout> 

這給了我下面,你會發現一個非常小的線左右的每個項目。我也想擺脫。

enter image description here

+0

你要保證金或填充? – Sam 2013-03-11 15:39:03

+1

創建圓角和邊框並不難,本[開發人員指南](https://developer.android.com/guide/topics/resources/drawable-resource.html#Shape)解釋了一些細節。 – Sam 2013-03-11 15:44:56

+0

我想要保證金。我希望我的列表項分開。 – CJe 2013-03-11 15:55:25

回答

59

我不是偉大的佈局,但我已經在過去的發現該ListView行通常會忽略LayoutParams。我不知道在哪裏發生這種情況,或者如果它是可以覆蓋的,我知道,你可以很容易地解決它通過添加另一個佈局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:background="#990000ff" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginRight="10dp" 
     android:layout_marginTop="10dp" 
     android:background="#9900ff00" 
     android:paddingLeft="10dp" > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#99ff0000" /> 
    </LinearLayout> 

</LinearLayout> 

典型的佈局,只有一個孩子可以刪除,但你可以看到這樣一個服務的目的:

Screen Shot

最外面的佈局是藍色的,TextView的是紅,綠是多餘的佈局,使您可以添加一些額外的間距。注意填充(左邊的綠色)和邊距(右邊沒有綠色)之間的區別。你已經清楚地表明你想使用邊距(android:layout_margin),但你的代碼明顯使用了填充(android:padding),所以我包括了這兩個。

+0

謝謝薩姆。我想我已經做了或多或少的描述(增加一個額外的佈局)。 AFAICS你與右邊的線條有相同的問題(也可能離開)。任何想法如何擺脫這些? – CJe 2013-03-11 17:19:20

+2

「非常小的一行」你的意思是[這些](http://i.stack.imgur.com/KnXaV.png)?這些是可以通過將'android:divider'更改爲空或透明圖像或可能設置'android:dividerHeight =「0dp」'將其刪除的分隔符。 – Sam 2013-03-11 17:22:40

+0

在外部RelativeLayout上使用填充的原因等同於在內部RelativeLayout上使用邊距不是嗎?我會盡力扭轉它。 – CJe 2013-03-11 17:22:41

4

在您的適配器,趕上getView)的相對佈局(,然後給一個佈局參數,可以

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)view.getLayoutParams(); 
params.setMargins(80, 0, 0, 0); //substitute parameters for left, top, right, bottom 
YourRelativeLayoutInAdapter.setLayoutParams(params); 
+0

謝謝。會放棄它。沒有辦法通過XML來做到這一點?如果是這樣,爲什麼不呢? – CJe 2013-03-11 15:44:05

+1

我完全不知道它,但是當我通過使用xml嘗試它之前。我也沒有爲我工作。所以我通過在適配器中提供餘量來解決這個問題。 – Talha 2013-03-11 15:50:36

+0

無法使其正常工作。更新的問題。 – CJe 2013-03-11 15:56:02

15

有點晚看到這一點,但只是以下行添加到您的ListView XML元素

android:divider="#00000000" 
android:dividerHeight="10dp" 
0

添加layout_widthListView,並把它放在中心,或通過margin移動。

(也許android:layout_centerHorizontal="true"。)

它的工作非常適合我。

0

我想你有一個在某個XML文件中定義的ListView,如果是的話,你可以添加一些padding它,這樣在屏幕邊緣和ListView之間會有一些空間。

例子:

<ListView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="15dp"/>