1

我面临嵌套布局问题并抛出一些异常。错误是“此LinearLayout布局或其LinearLayout父项无用......”。我知道我可以通过这个设置忽略这个警告。LinearLayout或其LinearLayout父项无用,我可以忽略警告消息吗?

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

但是,Eclipse图形布局无法显示任何内容并显示错误消息 - “索引:0,大小0,异常详细信息记录在窗口>显示视图>错误日志中”。

我怎样才能使图形布局显示的巢布局?

这里是我的代码:

<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> 

感谢所有。

+0

什么错误显示'错误Log'? – Jin35 2012-07-18 08:48:22

+2

这里是什么使用第二个布局....... – 2012-07-18 08:49:44

+0

添加一个更多的视图在父布局(可能是空白或一些可用如果有的话)删除警告。在这段代码中,水平布局没有用处。 – Kamal 2012-07-18 08:54:45

回答

1

你有两个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> 
+0

是的,我明白了。事实上,为什么我用这种方式编写的是我想要将图像按钮设置为从底部移动到顶部。我不想硬编码这个动画。我能怎么做?你有最好的方法来做到这一点? – KingWu 2012-07-18 09:22:21

+0

你可以用单个LinearLayout来做到这一点。很多例子都在那里。例如[这篇文章](http://stackoverflow.com/q/4526288/593709)处理从下到上的动画按钮 – 2012-07-18 09:28:38

0

你可以尝试

<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" > 
+0

我是懒惰的男孩= P。我想将小部件拖放到布局中。哈哈= P – KingWu 2012-07-18 09:15:20

+0

对不起。有用。但是,我真的想要嵌套布局是“Wrap_content”。 – KingWu 2012-07-18 09:19:24

+0

尝试在layout_height上放置wrap_content。 – rxlky 2012-07-18 09:28:02

0

您已经打开了2个linearlayout,但只有1个linearlayout关闭。

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

+0

mmm,由于结构不完整而导致关闭“”丢失。但它在那里,我解决了这个问题。 :) – 2012-07-18 09:05:36

+0

因为我想动画第二个盒子。我认为嵌套布局可以做到这一点。所以我继续前进。你有什么主意吗? – KingWu 2012-07-18 09:12:25

+0

Adil Soomro:你的意思是关闭失踪?实际上,我的XML有两个关闭。我怎么了? – KingWu 2012-07-18 09:14:11

3

因为在第一个LinearLayout中,它只有一个视图,并且它是一个LinearLayout。

这里是示出结构:

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

的LinearLayout是ViewGroup中的一个子类,并且旨在将一些视图(S),至少2个(CMIIW)。

因此,发出警告是因为它认为您的第一个LinearLayout没有任何组或者没有任何关系,如果您省略它。

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

0

也许帮助你:

android:layout_alignWithParentIfMissing ="true" 
+3

我认为这将是更多的OP和更多的访问者,当你添加一些解释你的内涵。 – reporter 2014-09-23 11:26:30

相关问题