将 header 添加到宽度不匹配 parent 的 ListView
Adding header to ListView not matching parent in width
各个布局文件看起来不错(match_parent
实际上填充了 parent),但是当我尝试设置 ListView
的 header 时,它的行为有所不同。
我的看法是:按钮的宽度与其 parent 布局的宽度相匹配,不知道为什么这不起作用。
实际:
预计:
我无法在此处设置代码格式,所以这里是它们的 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);
如 this pro-tip and confirmed in the documentation for View.inflate()
中所述,当您使用 View.inflate(MainActivity.this, R.layout.header_layout, null)
时,您最顶层视图中的所有 layout_
属性都将被有效忽略,因为您没有指定父视图 - 默认值layout_height
和 layout_width
是 wrap_content
.
相反,您应该调用 inflate(int, ViewGroup, boolean) method in LayoutInflater,传递父级和 false
以不附加视图(因为这就是 addHeaderView()
将为您做的):
LayoutInflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
trackingHeader = (LinearLayout) inflater.inflate(
R.layout.header_layout, trackingList, false);
header 的宽度与 ListView
属性相同,因此您需要更改
android:layout_width="wrap_content"
至
android:layout_width="match_parent"
各个布局文件看起来不错(match_parent
实际上填充了 parent),但是当我尝试设置 ListView
的 header 时,它的行为有所不同。
我的看法是:按钮的宽度与其 parent 布局的宽度相匹配,不知道为什么这不起作用。
实际:
预计:
我无法在此处设置代码格式,所以这里是它们的 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);
如 this pro-tip and confirmed in the documentation for View.inflate()
中所述,当您使用 View.inflate(MainActivity.this, R.layout.header_layout, null)
时,您最顶层视图中的所有 layout_
属性都将被有效忽略,因为您没有指定父视图 - 默认值layout_height
和 layout_width
是 wrap_content
.
相反,您应该调用 inflate(int, ViewGroup, boolean) method in LayoutInflater,传递父级和 false
以不附加视图(因为这就是 addHeaderView()
将为您做的):
LayoutInflater inflater = (LayoutInflater) getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
trackingHeader = (LinearLayout) inflater.inflate(
R.layout.header_layout, trackingList, false);
header 的宽度与 ListView
属性相同,因此您需要更改
android:layout_width="wrap_content"
至
android:layout_width="match_parent"