【问题标题】:How do I add margin values to inflated ListView?如何将边距值添加到膨胀的 ListView?
【发布时间】:2017-09-03 06:59:43
【问题描述】:

这是我的每行外观代码。

<?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>

现在每一行都在一起了,left,right,bottom,top之间没有间距。

如果我向列表视图添加边距或填充,它仅适用于整个列表,而不适用于每一行。

怎么做?我知道我可以在每一行之间添加分隔线,但我也想要左右间距。

这是来自自定义适配器的 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());
}

【问题讨论】:

  • 您能否在代码中显示您正在为您的项目增加视图的位置? getView 方法。
  • 我现在编辑并添加了getview方法
  • 好的,我解决了。我以前试过这个,但没有用,所以可能是我检查错了,或者 android studio 预览没有正确显示。我刚刚在第一个 xml 周围添加了另一个具有相同高度、宽度的 LinearLayout,现在它可以工作了。
  • 您也可以尝试切换到 RecyclerView - 它可能在单元格顶部布局参数方面更加一致。

标签: android listview android-linearlayout


【解决方案1】:

如果要为 ListView 添加边距,请向列表视图添加边距。 如果要为项目添加边距,请为项目添加边距。

这很简单。

但是,我建议您使用 ConstraintLAyout。和 RecyclerView 而不是 ListView。

【讨论】:

    【解决方案2】:

    尝试在 LinearLayout 中使用 padding,并创建另一个 LinearLayout 作为原始的子级,并将背景属性移动到它。

    【讨论】:

      【解决方案3】:

      尝试使用以下方法在 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);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-04
        • 1970-01-01
        • 2023-03-27
        • 2014-03-27
        相关资源
        最近更新 更多