android - ExpandableListView.addHeaderView() 没有保留布局参数

标签 android android-layout android-listview expandablelistview

我有一个 RelativeLayouts,我想将其作为标题添加到 ExpandableList。基本上,布局与我用于列表中 subview 的布局相同。

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="72dp"
    android:paddingTop="16dp"
    android:paddingBottom="16dp"
    android:paddingLeft="50dp"
    android:paddingStart="50dp"
    android:paddingRight="16dp"
    android:paddingEnd="16dp">

    <TextView
        android:id="@+id/category_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Electricity"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>

    <TextView
        android:id="@+id/current_balance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:text="Rp 300.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Subhead"/>

    <TextView
        android:id="@+id/current_budget"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Rp800.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/secondary_text"/>

    <TextView
        android:id="@+id/current_expenses"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:text="Rp800.000"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        android:textColor="@color/secondary_text"/>

</RelativeLayout>

然后我在 onCreateView 中添加 header ,如下所示:

View view = inflater.inflate(R.layout.fragment_budget_list, container, false);

View listHeader = inflater.inflate(R.layout.row_budget_item, null);
// populate views in the header

ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.list_budget);
listView.addHeaderView(listHeader);
listView.setAdapter(mAdapter);

显示了标题,但是,看起来高度已更改为 0,因为第一行中的 TextView(category_name 和 current_balance)与第二行(current_budget 和 current_expenses)重叠。

(注意:布局在用作列表的 subview 时正确显示)

这是因为布局被转换为 AbsListView 了吗? 如何保留布局参数? 我试过 listHeader.setMinimumHeight() 但它不起作用

最佳答案

从这个 post 找到我的答案

显然,在膨胀页眉布局时,我需要提供 ExpandableListView 作为根,并向 attachToRoot 提供 false。因此,布局参数值将提供给 Root View 。所以代码看起来像这样:

View view = inflater.inflate(R.layout.fragment_budget_list, container, false);
ExpandableListView listView = (ExpandableListView) view.findViewById(R.id.list_budget);

View listHeader = inflater.inflate(R.layout.row_budget_item, listView, false);

来自docs :

root: Optional view to be the parent of the generated hierarchy (if attachToRoot is true), or else simply an object that provides a set of LayoutParams values for root of the returned hierarchy (if attachToRoot is false.)

关于android - ExpandableListView.addHeaderView() 没有保留布局参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29474243/

相关文章:

android - 使用 addView 而不是 listView

android - 在 Android 上保持 Activity Activity 并正常退出

android - 检测到 '@' 时生成 ListView

android - 如何在布局而不是菜单项中显示 ShareActionProvider?

java - Android:在 ListView 中将整数读取为字符串

android - 使用 setAdapter 后如何防止 ListView 滚动到顶部

android - 如何在 Android Studio 中正确使用 Git?

android - 在 TabActivity 中切换到新选项卡时关闭 Android PopupWindow?

android - 为自定义 ActionBar 按钮实现 OnClick 方法

Android:检测水平 ScrollView 何时停止滚动