【问题标题】:LinearLayout Problem (2 LLayout in the same XML)LinearLayout问题(同一个XML中的2个LLayout)
【发布时间】:2011-06-20 15:24:33
【问题描述】:

我正在为设计一个 android 应用程序编写新的 XML 文件,我在同一个 xml 中使用 2 个 linearLayour 时遇到了一些问题...

我使用以下代码出现“XML 文件中的错误:正在中止构建”:

        <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

  <TextView android:id="@+id/texte_firsttab"   
    android:layout_height="wrap_content"   
    android:layout_width="wrap_content"/>

</LinearLayout>

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">


      <Button android:id="@+id/accessGraphe"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="test"
    android:onClick="selfDestruct" />

  <Button android:id="@+id/accessGraphe2"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:text="test2"
    android:onClick="selfDestruct" />
</LinearLayout>

我们可以将两个线性布局放在同一个xml文件中吗?

【问题讨论】:

    标签: android xml android-linearlayout


    【解决方案1】:

    是的,你可以。但不是两个顶级。
    看这个例子:http://developer.android.com/resources/tutorials/views/hello-linearlayout.html

    【讨论】:

    • 完美,我已将它们放在其他线性布局中;-) 谢谢!现在我明白为什么按钮不想显示了^^
    【解决方案2】:

    您不能有两个顶级布局。系统如何知道如何安排它们?您需要将它们包含在另一个定义它的布局中。

    我假设&lt;?xml ... 标记前面的空白是由于您的帖子中的代码格式造成的,并且实际布局文件中不存在。这也会导致问题。

    【讨论】:

      【解决方案3】:

      不,你不能......你如何从源代码中引用其中一个或另一个?

      如果您想同时拥有两个线性布局(一个在顶部,一个在底部),那么您需要将它们嵌入到另一个布局中:

      <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
      <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weigth="1">
      
        <TextView android:id="@+id/texte_firsttab"   
          android:layout_height="wrap_content"   
          android:layout_width="wrap_content"/>
      
      </LinearLayout>
      
      <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
          android:layout_weigth="1" >
      
      
            <Button android:id="@+id/accessGraphe"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="test"
          android:onClick="selfDestruct" />
      
        <Button android:id="@+id/accessGraphe2"
          android:layout_height="wrap_content"
          android:layout_width="wrap_content"
          android:text="test2"
          android:onClick="selfDestruct" />
      </LinearLayout>
      </LinearLayout>
      

      【讨论】:

        【解决方案4】:

        您有两个根级项目,这是不应该发生的(这并不特定于 Android 布局文件,任何 XML 文件中只能有一个文档元素)。

        我不建议将到 LinearLayouts 包装在另一个中,这太复杂了;避免嵌套布局通常是个好主意,请参阅this article 了解高效布局。

        对于一个 TextView 和两个 Button,RelativeLayout 将是完美的。它也比 LL 更灵活。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-09-06
          • 1970-01-01
          • 2022-06-13
          • 1970-01-01
          • 2023-04-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多