2

我正在尝试使用 Fragments 在 Android 中进行布局。我开始使用 Commonsware 的MergeAdapter,但遇到了一些奇怪的麻烦。布局以前工作得很好,但现在我明白了:

http://img856.imageshack.us/img856/2796/layoutbug.png

有几个问题:白色条带的文本应该一直贯穿其长度。我将 TextView 设置为白色背景,以确保正确设置宽度。它下面的复选框也应该说“问题?” 但它出于某种原因试图包装文本。

这是正在添加的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
    <TextView android:id="@+id/tvInstructions" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
        <CheckBox android:id="@+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="0dp" android:text="Issues?" />
        <TextView android:id="@+id/tvStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5" android:text="Station:" />
        <Spinner android:id="@+id/spStation" android:layout_height="wrap_content" android:layout_width="0dp" android:layout_weight=".5"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>

这就是我如何在 Fragment 的 onActivityCreated 中膨胀它:

LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
MergeAdapter _adapter = new MergeAdapter();

View observationHeaderView = inflater.inflate(R.layout.observationheader, getListView(), false);
_adapter.addView(observationHeaderView);

我觉得这与我如何夸大布局有关。任何帮助,将不胜感激。谢谢。

4

2 回答 2

1

不要将match_parentforandroid:layout_height用于进入 a 的内容AdapterView,例如您的 root LinearLayout

您也可以考虑暂时将您的标题布局ListView放在.LinearLayoutListViewListView

除此之外,启动 Hierarchy View(Eclipse 透视图或独立版)并尝试找出您的布局规则哪里出错了。

另外,我不确定您在蜂窝Spinners内的工作情况如何。ListView

于 2011-09-28T00:24:23.273 回答
0

看来我的问题是layout_widthmatch_parent我的标题中的某些地方,当它应该是wrap_content. 以下工作:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions" />
    <TextView android:id="@+id/tvInstructions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Instructions go here" android:textSize="32dp" android:background="#FFFFFF"></TextView>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:orientation="horizontal" android:paddingTop="24dp">
        <CheckBox android:id="@+id/cbIssues" android:layout_height="wrap_content" android:layout_weight="1" android:layout_width="wrap_content" android:text="Issues?" />
        <TextView android:id="@+id/tvStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5" android:text="Station:" />
        <Spinner android:id="@+id/spStation" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_weight=".5"/>
    </LinearLayout>
    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Pending data" />
</LinearLayout>
于 2011-09-29T19:39:34.953 回答