【问题标题】:Inflating RelativeLayout within another RelativeLayout在另一个 RelativeLayout 中膨胀 RelativeLayout
【发布时间】:2018-08-23 03:53:11
【问题描述】:

我正在尝试将自定义列表视图的布局模型包含在单个自定义列表视图 layout.xml 文件中,并使用使用 kotlin 的自定义适配器类对其进行扩展。

数据似乎很好,行格式也很准确,只是每行都重复了标题。我猜我可以将标题从列表视图 layout.xml 文件中移出,但我更愿意保持它自包含以便可以重复使用。

这是有问题的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- Header aligned to top -->

<RelativeLayout
    android:id="@+id/estimated_ftp_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:background="@color/colorHeader"
    android:gravity="start" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="15dp"
        android:text="@string/estimated_ftp"
        android:textColor="#000"
        android:textSize="20sp" />
</RelativeLayout>

<!-- Content below header -->

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:layout_below="@id/estimated_ftp_header"
    android:orientation="horizontal"
    android:padding="6dip"
    android:id="@+id/estimated_ftp_row">

    <ImageView
        android:contentDescription="@string/ftp_type"
        android:id="@+id/ftp_icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/centerPoint"
        android:gravity = "center|end|end"
        android:src="@drawable/glyphicons_13_heart"/>

    <TextView
        android:id="@+id/centerPoint"
        android:text=""
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" />

    <TextView
        android:id="@+id/ftp_value"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/centerPoint"
        android:gravity = "center|start|start"
        android:text="141 bpm"/>

</RelativeLayout>

我相信解决方案是弄清楚如何选择estimated_ftp_row id 并将其膨胀而不是整个estimated_fto.xml 文件。

这是适配器中的代码,它在 getView 方法中膨胀之前选择布局:

val inflater = activity?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
view = inflater.inflate(R.layout.estimated_ftp, null)
viewHolder = ViewHolder(view)
view?.tag = viewHolder

如何更改 inflater.inflate 以在 R.layout.estimated_ftp 中充气?

FWIW,活动的 XML 布局在用作占位符的位置有一个 ListView 控件。它不引用自定义布局文件。该连接是通过上面提到的适配器类完成的。不确定这是否有问题,所以我想提一下...

提前感谢您的任何建议。

麦克

解决方案更新: https://stackoverflow.com/users/2850651/mike-m 有我需要的指导。我能够将行格式与标题格式 XML 布局分开。适配器未更改,如果找到相关列表的内容,则 Activity 具有添加标头的新代码。

val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)

【问题讨论】:

  • 您不能只对布局的一部分进行充气。当你给一个充气时,它就是全部。您需要有两个单独的布局;一个用于标题,一个用于项目。 ListView 支持与项目分开的标题,因此您只需添加该标题 View 一次,而不是尝试以某种方式从除第一个项目之外的每个项目中删除它。另外,我不确定您所说的重用究竟是什么意思,但如果您需要将这些布局组合为应用程序的其他部分中的一个,那么您可以随时将其中一个扩展为另一个。
  • 感谢您的提示。我现在将使用您的解决方案编辑帖子。
  • 请将解决方案发布为答案,这样人们就知道问题已解决。见stackoverflow.com/help/self-answer

标签: android android-layout kotlin android-relativelayout


【解决方案1】:

解决方案:

https://stackoverflow.com/users/2850651/mike-m 得到了我需要的指导。我能够将行格式与标题格式 XML 布局分开。适配器未更改,如果找到相关列表的内容,则 Activity 具有添加标头的新代码。

val headerView = layoutInflater.inflate(R.layout.estimated_ftp_head, null)
estimated_ftp_list.addHeaderView(headerView)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-23
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多