2017-09-03 20 views
0

這是我的每行看起來的代碼。如何將邊距值添加到膨脹的ListView?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="120dp" 
    android:id="@+id/recipe_container" 
    android:layout_margin="12dp" 
    android:background="#f1f1f1"> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/recipe_name_text_view" 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:gravity="center_vertical" 
      android:textSize="32sp" 
      android:text="DUMMY TEXT"/> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="60dp" 
      android:layout_weight="1" 
      android:gravity="center_vertical" 
      android:orientation="horizontal"> 


      <TextView 
       android:id="@+id/recipe_time_text_view" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="DUMMY TEXT"/> 

      <TextView 
       android:id="@+id/recipe_difficulty_text_view" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="DUMMY TEXT"/> 

     </LinearLayout> 

    </LinearLayout> 


    <ImageView 
     android:id="@+id/recipe_thumbnail_image_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/sladice"/> 


</LinearLayout> 

正如你可以看到我有保證金設置,如果我檢查預覽它也顯示。

我填充此listivew填充。 這是activity_sladice.xml。

<?xml version="1.0" encoding="utf-8"?> 
<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/recipe_list" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.android.recepti.Sladice" 
    android:background="@color/colorPrimary"> 

</ListView> 

現在每行都在一起,左,右,底,頂之間沒有間距。

如果我將margin或padding添加到listview,它只能在整個列表上工作,而不是在每一行上。

怎麼辦?我知道我可以在每行之間添加分隔線,但我也想要左右分隔。

這是自定義適配器的getView方法。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    View recipeView = convertView; 
    ReceptThumbnail recipe = getItem(position); 

    // Inflate view if to be inflated 
    if (recipeView == null) { 
     recipeView = LayoutInflater.from(getContext()).inflate(R.layout.recipe_item, parent, false); 
    } 

    TextView recipeName = (TextView) recipeView.findViewById(R.id.recipe_name_text_view); 
    recipeName.setText(recipe.getName()); 

    TextView recipeTime = (TextView) recipeView.findViewById(R.id.recipe_time_text_view); 
    recipeTime.setText(recipe.getTime()); 

    TextView recipeDifficulty = (TextView) recipeView.findViewById(R.id.recipe_difficulty_text_view); 
    recipeDifficulty.setText(recipe.getDifficulty()); 

    ImageView recipeImage = (ImageView) recipeView.findViewById(R.id.recipe_thumbnail_image_view); 
    recipeImage.setImageResource(recipe.getImageResourceID()); 
} 
+0

您能否在代碼中顯示您的物品充氣視圖的代碼? getView方法。 – algrid

+0

我現在編輯並添加了getview方法 – user3366345

+0

好吧,我解決了它。我之前嘗試過,但它沒有工作,所以也許我檢查錯誤或android studio預覽不能正確顯示。我剛剛在第一個xml中添加了另一個LinearLayout,它具有相同的高度,寬度,現在它可以工作。 – user3366345

回答

0

如果您想爲ListView添加邊距,請將邊框添加到列表視圖中。 如果要爲項目添加邊距,請爲項目添加邊距。

很簡單。

但是,我建議你使用ConstraintLAyout。和RecyclerView而不是ListView。

0

嘗試在LinearLayout中使用填充,並創建另一個LinearLayout作爲原始的子項,然後將背景屬性移動到它。

0

嘗試使用下面的方法在getView方法中以編程方式設置邊距。

public static void setMarginLeft(View v, int left) { 
    ViewGroup.MarginLayoutParams params = 
           (ViewGroup.MarginLayoutParams)v.getLayoutParams(); 
    params.setMargins(left, params.topMargin, 
          params.rightMargin, params.bottomMargin); 
} 
相關問題