使 ExpandableListView 尊重项目边距和填充

Making ExpandableListView respect item margins and padding

我有一个正在填充项目的 ExpandableListView。我想让 header(这里是 "meat")比列表项更宽。

但是,ExpandableListView 似乎不遵守 XML 中定义的 margins/padding,并且也不遵守项目的 programatically-set 填充。它确实修改了 parent ("meat") 但相同的代码不适用于项目。

我该怎么做?

这是我的自定义适配器的相关部分:

 @Override
 public View getGroupView(int position, boolean isExpanded, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_category, null);
    }

    view.setPadding(10, 0, 10, 0); //RESPECTED

    initializeGroupComponents(view, getGroup(position));
    return view;
}

@Override
public View getChildView(int position, int expandedPosition, boolean isLastChild, View view, ViewGroup parent) {
    if (view == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.list_ingredient, parent, false);
    }

    view.setPadding(10, 2, 10, 2); //DOES NOT WORK

    initializeChildComponents(view, getChild(position, expandedPosition));
    return view;
}

我XML的相关部分:

群组:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_categoryContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:orientation="vertical">

项目:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="62dp"
android:layout_marginBottom="1dp"
android:layout_marginEnd="6dp"
android:layout_marginStart="6dp"
android:background="@android:color/white"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="10dp">

为 header 使用一个额外的布局并添加边距

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

    <LinearLayout 
    android:layout_marginTop="5dp"
    android:layout_marginEnd="5dp"
    android:layout_marginStart="5dp">

    //other content
    </LinearLayout>
    </LinearLayout>