2012-12-27 44 views
4

是否有可能有邊緣/填充的ListView沒有應用到頁眉的邊距?我希望我的ListView像Google Now佈局一樣。除了這個小問題外,我已經做好了一切工作。Android:邊界/填充的ListView沒有應用到頁眉的邊距?

問題:是否有可能沒有ListView的佈局參數適用於其頭部?

編輯1:更多信息

紫色視圖是listView的標題。 我已經嘗試添加邊距到列表項目layout.This也不工作。 我需要的是一種將邊距應用到列表視圖的方法,以便它不會將邊距應用於listView中的標題。

EDIT2: 我的佈局 header.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:orientation="vertical" > 

    <View 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/top_height" 
     android:background="@color/top_item" /> 

    <View 
     android:id="@+id/placeholder" 
     android:layout_width="match_parent" 
     android:layout_height="@dimen/sticky_height" 
     android:layout_gravity="bottom" /> 

</FrameLayout> 

root_list.xml

<LinearLayout> 
<com.abhijith.CustomListView 
    android:id="@android:id/list" 
    android:layout_marginLeft="16dp" 
    android:layout_marginRight="16dp" 
    android:layout_height="wrap_content" /></LinearLayout> 

list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/card_background" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/textview_note" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" /> 

<TextView 
    android:id="@+id/textview_note_date" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/></LinearLayout> 

我由於此處被拆除uselsess佈局屬性這篇文章的長度。 This is how it looks

+1

應用填充同樣爲所有項目和不適填充到頁眉查看。 – TNR

+0

我認爲你的意思是增加邊界到listItem layout.It沒有工作。 – redshift13

+0

我的意思是添加填充到每個列表項目佈局項目。並且不要將其添加到標題。 – TNR

回答

7

經過大量的挖掘,我發現它不能將listView佈局參數從它的頭文件中解耦出來,並且頭文件是listView的一部分。因此,爲了得到預期的邊際效應,經過多次觀察,我發現在列表視圖項目的佈局的根目錄中添加頁邊空白並不奏效。因此,我在現有的根LinearLayout(一個嵌套的LinearLayout)內添加了另一個虛擬LinearLayout,並在嵌套佈局中添加了子元素。現在,設置內部佈局的邊距可獲得所需的效果。

代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="16dp" 
     android:layout_marginLeft="16dp" 
     android:background="@drawable/list_selector" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/textview_note" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 

     <TextView 
      android:id="@+id/textview_note_date" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" /> 
    </LinearLayout> 

</LinearLayout> 
+3

您可以刪除外部的LinearLayout,而不是'android:layout_marginRight =「16dp」 android:layout_marginLeft =「16dp」'put'android:paddingLeft =「16dp」 android:android:paddingRight =「16dp」'或者至少使用外部FrameLayout而不是LinearLayout – vovahost

-1

你可以通過在listview的佈局文件中給予margin來做到這一點,那麼你會得到它的輸出。

+1

在列表視圖佈局文件中添加保證金讓我在這裏。我不認爲你明白這個問題。 – redshift13

+0

可以請你解釋我更多的垃圾,所以我會幫你嗎? –

+0

問題用更多信息更新 – redshift13

0

在頭部分,添加以下代碼:

android:layout_alignParentLeft="true" 

這將解決你的問題。

+0

我的頭文件是一個xml,我以編程方式膨脹並通過listView.addHeaderView(headerView)添加列表。標題綁定到listView。所以改變列表視圖的邊距顯然會改變標題。我在header.xml上試過了你的建議,但它不起作用。 – redshift13

+0

你在根上使用什麼樣的佈局? – Rohit

+0

只有當您在列表視圖外使用相對佈局時才能使用。 – Rohit

0
// Temp is the root view that was found in the xml 
final View temp = createViewFromTag(root, name, attrs, false); 

ViewGroup.LayoutParams params = null; 

if (root != null) { 
    if (DEBUG) { 
     System.out.println("Creating params from root: " + 
       root); 
    } 
    // Create layout params that match root, if supplied 
    params = root.generateLayoutParams(attrs); 
    if (!attachToRoot) { 
     // Set the layout params for temp if we are not 
     // attaching. (If we are, we use addView, below) 
     temp.setLayoutParams(params); 
    } 

// We are supposed to attach all the views we found (int temp) 
// to root. Do that now. 
if (root != null && attachToRoot) { 
    root.addView(temp, params); 
} 

// Decide whether to return the root that was passed in or the 
// top view found in xml. 
if (root == null || !attachToRoot) { 
    result = temp; 
} 

... 
return result; 

這是LayoutInflater的代碼,你可以看到,如果添加root,它不爲空,產生從根layoutParams

然後如果第三參數是虛假,只有製造temp.setLayoutParams(params)其中params是由根生成的!和溫度是作爲返回....

如果attachToRoot,根將執行root.addView(temp,params),然後根作爲返回..

,如果你只是做LayoutInflater.from(this).inflate(R.layout.popup_choose_type, null);,如果您設置了一些屬性作爲paddingBottom,它會驚訝你是不會因爲損失的工作!的LayoutParams

至於如何解決這個問題,你可以看到 here