【问题标题】:MergeAdapter layout issue in a fragment片段中的 MergeAdapter 布局问题
【发布时间】:2011-09-27 23:41:49
【问题描述】:

我正在尝试使用 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);

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

【问题讨论】:

    标签: android android-layout android-fragments commonsware-cwac


    【解决方案1】:

    不要将match_parent 用于android:layout_height 用于进入AdapterView 的内容,例如您的根LinearLayout

    您还可以考虑暂时将您的标题布局放在ListView 之外(例如,在垂直的LinearLayout 中也包含ListView)并在添加将其放入ListView 的复杂性之前对其进行调试。

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

    另外,我不确定您的 Spinners 在 Honeycomb 上的 ListView 内的效果如何。

    【讨论】:

    • 更改 layout_height 并不能解决问题。我将标题放在 ListView 之外,它工作得很好。它只在 ListView 内执行此操作。我在来这里之前尝试了 Hierarchy View,但我不知道发生了什么。据我所知,该元素是正确的宽度。微调器工作正常。
    • 如果我不向 ListAdapter 添加任何其他视图,它工作正常。
    • @Andrew Koester:如果您创建一个完整的示例项目来演示该问题,我可以看一下。
    • 啊哈。在创建示例项目时,它似乎工作正常,直到我从其中一个 Spinners 中选择了一个值。我认为这是我的问题的根源,我将用一个按钮和一个单选按钮警报对话框替换它。
    • 陌生人和陌生人。一旦我更改了值,它仍然使用文本框。
    【解决方案2】:

    看来我的问题是layout_width 在我的标头中的某些位置是match_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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多