【问题标题】:Adding header to ListView not matching parent in width向 ListView 添加标题的宽度与父级不匹配
【发布时间】:2016-04-09 21:07:17
【问题描述】:

各个布局文件看起来不错(match_parent 实际上填充了父级),但是当我尝试设置 ListView 的标题时,它的行为有所不同。

我看到它的方式是:按钮的宽度与其父布局的宽度匹配,不知道为什么这不起作用。

实际:

预期:

我无法让此处的代码格式正常工作,因此这里有指向它们的 pastebin 链接。

header_layout.xml

<LinearLayout 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"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

    <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:paddingTop="30dp"
       android:orientation="vertical"
       android:id="@+id/trackingHeader">

        <LinearLayout
           android:layout_width="0px"
           android:layout_height="0px"
           android:focusable="true"
           android:focusableInTouchMode="true" />

        <Button
           android:id="@+id/start_service"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_below="@+id/textView"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="10dp"
           android:text="Begin Tracking" />

        <Button
           android:id="@+id/stop_service"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:layout_below="@+id/start_service"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="10dp"
           android:text="Cease Tracking" />

        <TextView
           android:id="@+id/trackingStatus"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center_horizontal"
           android:text="Large Text"
           android:textAppearance="?android:attr/textAppearanceLarge"
           android:paddingTop="10dp"
           android:paddingBottom="10dp" />

    </LinearLayout>
</LinearLayout>

content_main.xml

<LinearLayout 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"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity">

    <ListView
       android:id="@+id/trackingList"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

</LinearLayout>

MainActivity.java

    setContentView(R.layout.activity_main);
    trackingHeader = (LinearLayout) View.inflate(MainActivity.this, R.layout.header_layout, null);
    trackingList = (ListView) findViewById(R.id.trackingList);
    trackingList.addHeaderView(trackingHeader, null, false);

【问题讨论】:

  • android:layout_width="0px"改成android:layout_width="match_parent"?
  • 你说的是header_layout.xml中的那个吗?我最初把它放在那里是为了转移我之前的一些文本框的注意力。我删除了它,它没有效果
  • 啊,抱歉,我正在将您的问题编辑为正确的格式,但我误以为这是您的布局。您是否尝试将您的 ListView 宽度更改为 match_parent
  • 哦,原来如此!根据它的外观,我认为它已经是 match_parent 。想要发布该答案以便我可以将此问题标记为已解决?
  • 我不确定答案,它是从逻辑推理中得出的,所以我将其发布为评论而不是答案。我现在已将其添加为答案。通过解决您的问题,我也意识到它是如何工作的 :)

标签: android xml android-layout android-listview


【解决方案1】:

正如this pro-tip 中所述并在documentation for View.inflate() 中确认的那样,当您使用View.inflate(MainActivity.this, R.layout.header_layout, null) 时,由于您没有指定父级,因此您最顶层视图上的所有layout_ 属性都将被有效忽略 - 的默认值layout_heightlayout_widthwrap_content

相反,您应该在LayoutInflater 中调用inflate(int, ViewGroup, boolean) 方法,传入父级和false 以不附加视图(因为这就是addHeaderView() 将为您做的):

LayoutInflater inflater = (LayoutInflater) getSystemService
    (Context.LAYOUT_INFLATER_SERVICE);
trackingHeader = (LinearLayout) inflater.inflate(
    R.layout.header_layout, trackingList, false);

【讨论】:

    【解决方案2】:

    header的宽度和ListView属性一样,所以需要修改

    android:layout_width="wrap_content"
    

    android:layout_width="match_parent"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2018-10-05
      • 2015-10-13
      • 2019-08-22
      • 2017-05-25
      • 1970-01-01
      相关资源
      最近更新 更多