Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将标题添加到ListView的宽度与父项不匹配_Android_Xml_Android Layout_Android Listview - Fatal编程技术网

Android 将标题添加到ListView的宽度与父项不匹配

Android 将标题添加到ListView的宽度与父项不匹配,android,xml,android-layout,android-listview,Android,Xml,Android Layout,Android Listview,单个布局文件看起来不错(match\u parent实际上填充了parent),但是当我尝试设置ListView的标题时,它的行为不同 我的看法是:按钮的宽度与其父布局的宽度相匹配,但不确定为什么这不起作用 实际: 预期: 我无法在这里设置代码格式,因此这里有指向它们的pastebin链接 header\u layout.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml

单个布局文件看起来不错(
match\u parent
实际上填充了parent),但是当我尝试设置
ListView
的标题时,它的行为不同

我的看法是:按钮的宽度与其父布局的宽度相匹配,但不确定为什么这不起作用

实际:

预期:

我无法在这里设置代码格式,因此这里有指向它们的pastebin链接

header\u 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>
<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);
如中所述,并在中确认,当您使用查看.充气(MainActivity.this,R.layout.header\u layout,null)时,由于您没有指定父视图,
layout\u高度
layout\u宽度
的默认值为
包裹内容
,因此,最顶部视图上的所有
layout\u属性都被有效忽略

相反,您应该在中调用该方法,传入父对象和
false
以不附加视图(因为
addHeaderView()
将为您执行此操作):

如中所述,并在中确认,当您使用查看.充气(MainActivity.this,R.layout.header\u layout,null)
时,由于您没有指定父视图,
layout\u高度
layout\u宽度
的默认值为
包裹内容
,因此,最顶部视图上的所有
layout\u属性都被有效忽略

相反,您应该在中调用该方法,传入父对象和
false
以不附加视图(因为
addHeaderView()
将为您执行此操作):


标题的宽度与
ListView
属性相同,因此需要更改

android:layout_width="wrap_content"


标题的宽度与
ListView
属性相同,因此需要更改

android:layout_width="wrap_content"


将android:layout\u width=“0px”
更改为android:layout\u width=“match\u parent”?您是说header\u layout.xml中的那个吗?我最初把它放在那里是为了从我之前的一些文本框中去掉焦点。我已经删除了它,它没有效果啊,对不起,我把你的问题编辑成了正确的格式,我误以为这是你的布局。您是否尝试将
列表视图的宽度更改为
match\u parent
?哦,就是这样!根据它的外观,我认为它已经是match_parent了。想发布那个答案,这样我就可以将这个问题标记为已解决?我不确定答案,它来自逻辑推理,所以我将它作为注释而不是答案发布。我现在把它作为答案加上去了。通过解决您的问题,我也意识到了它的工作原理:)将
android:layout\u width=“0px”
更改为
android:layout\u width=“match\u parent”
?您是说header\u layout.xml中的那个吗?我最初把它放在那里是为了从我之前的一些文本框中去掉焦点。我已经删除了它,它没有效果啊,对不起,我把你的问题编辑成了正确的格式,我误以为这是你的布局。您是否尝试将
列表视图的宽度更改为
match\u parent
?哦,就是这样!根据它的外观,我认为它已经是match_parent了。想发布那个答案,这样我就可以将这个问题标记为已解决?我不确定答案,它来自逻辑推理,所以我将它作为注释而不是答案发布。我现在把它作为答案加上去了。通过解决您的问题,我也意识到它是如何工作的:)
android:layout_width="match_parent"