【问题标题】:LinearLayout or its LinearLayout parent is useless , Can i ignore the warning message?LinearLayout 或其 LinearLayout 父级没用,我可以忽略警告消息吗?
【发布时间】:2012-07-17 06:15:14
【问题描述】:

我遇到了嵌套布局问题并引发了一些异常。错误是“此 LinearLayout 布局或其 LinearLayout 父级无用......”。我知道我可以通过这个设置忽略这个警告。

设置: Build Path->Configure Build Path.... 在 Android Lint Preferences 下查找 UselessParent 并将其严重性设置为忽略或单击 Ignore All。

但是,Eclipse 图形布局无法显示任何内容并显示错误消息 - “Index:0, Size 0, Exception details are logged in Window > Show View > Error Log”。

如何让图形布局显示嵌套布局??

这是我的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/menu_bg2"
        android:orientation="vertical" >

        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/menu_item1"
            android:src="@drawable/alarm2x" />

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/menu_item2"
            android:src="@drawable/bank2x" />

        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/menu_item3"
            android:src="@drawable/brief_case2x" />

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/menu_item4"
            android:src="@drawable/trolley2x" />

        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:contentDescription="@string/menu_item5"
            android:src="@drawable/calculator2x" />

    </LinearLayout>
</LinearLayout>

谢谢大家。

【问题讨论】:

  • Error Log 显示什么错误?
  • 这里第二个布局有什么用......
  • 在父布局中再添加一个视图(可能是空白的或一些可用的,如果有的话)以删除警告。在这段代码中,水平布局没有用。
  • Re Jin35:这是我的错误。 dl.dropbox.com/u/78582670/error.png Re RajaReddy:我想在线性布局中为 imageButton 播放一些翻译动画。我希望按钮从下到上移动。你有什么好主意吗? Re Kamal:水平布局很有用,因为我以后会在里面添加一些视图。

标签: android android-layout layout android-linearlayout android-lint


【解决方案1】:

因为在第一个LinearLayout中,里面只有1个view,是一个LinearLayout。

这是图示的结构:

LinearLayout
    LinearLayout
        ImageButton
        ImageButton
        ImageButton
        ImageButton
        ImageButton
    // You should add another View or ViewGroup for the first LinearLayout

LinearLayout 是 ViewGroup 的子类,旨在将一些 View 分组,至少 2 个 (CMIIW)。

因此,发出警告是因为它假定您的第一个 LinearLayout 没有任何要分组的内容,或者您​​忽略它也没关系。

对不起,如果我的英文解释不好。

【讨论】:

    【解决方案2】:

    你有两个LinearLayout,其中一个到现在都没用。此警告建议您删除其中一个。像这样:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/LinearLayout1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/menu_bg2"
              android:orientation="vertical" >
    
         <!-- your other ImageView etc.-->
    </LinearLayout>
    

    【讨论】:

    • 是的,我明白了。事实上,我这样写的原因是我想让图像按钮从下到上移动。我不想硬编码这个动画。我能怎么做?你有没有最好的方法来做到这一点?
    • 您可以使用单个 LinearLayout 来做到这一点。有很多例子。例如this post 处理从下到上的动画按钮
    【解决方案3】:

    您可以将其添加到您的父 LinearLayout:

    tools:ignore="UselessParent"
    

    默认情况下,如果这些警告无用,Android Studio 会显示这些警告。 tools:ignore="UselessParent" 阻止此警告出现。

    【讨论】:

      【解决方案4】:

      您已经打开了 2 个线性布局,但只有 1 个线性布局关闭。

      但是,第二个线性布局没有意义,因为您将一个框放在一个框内,然后在第二个中插入项目。

      【讨论】:

      • 嗯,结尾&lt;/LinearLayout&gt; 由于格式不正确而丢失。但它在那里,我修好了。 :)
      • 因为我想为第二个框设置动画。我认为嵌套布局可以做到这一点。所以我继续。你有什么想法吗?
      • Adil Soomro:你的意思是结尾的 不见了??事实上,我的 XML 有两个关闭的 。我怎么了?
      • @KingWu:您的结束标签在问题中不可见,由于格式错误,我再次格式化。所以现在它是可见的。
      • 那么布局没有问题。你能看看错误日志吗?窗口->显示视图->错误日志
      【解决方案5】:

      你可以试试

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/LinearLayout1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" >
      
      <LinearLayout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/menu_bg2"
          android:orientation="vertical" >
      

      但是我从不相信 Eclipse 中的图形布局。

      编辑:

      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/LinearLayout1"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal" >
      
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="vertical" >
      

      【讨论】:

      • 对不起。有用。但是,我真的希望嵌套布局是“Wrap_content”。
      • 尝试将 wrap_content 放在 layout_height 上。
      • 当我将背景图像添加到第二个布局中时,例如 android:background="@drawable/menu_bg2"。然后就不行了=[
      【解决方案6】:

      也许对你有帮助:

      android:layout_alignWithParentIfMissing ="true"
      

      【讨论】:

      • 我认为,当您为您的意图添加一些解释时,这对 OP 和更多访问者会更有帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多