【发布时间】: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