【问题标题】:This LinearLayout layout or its RelativeLayout parent is useless; transfer the background attribute to the other view warning这个 LinearLayout 布局或者它的 RelativeLayout 父级是没用的;将背景属性转移到其他视图警告
【发布时间】:2014-06-03 11:39:02
【问题描述】:

不确定我做错了什么,LinearLayout 中没有复杂的 2 个按钮,我得到了
“这个 LinearLayout 布局或它的 RelativeLayout 父级没用;将背景属性转移到另一个视图”警告..​​..这里是代码,让我大吃一惊

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/GCBS_Template_BKGND"
tools:context=".MainActivity" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <Button
        android:id="@+id/Partfinder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Part Finder" />

    <Button
        android:id="@+id/Heaterfinder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Heater Finder" />
</LinearLayout>

【问题讨论】:

  • 在该问题中接受的答案只是隐藏了警告。我并不是说您不能忽略警告,但您得到警告是有原因的。您的相对布局仅包含另一个布局。警告意味着其中一个没有用,您可以删除其中一个。

标签: android android-layout


【解决方案1】:

如果您只想垂直显示两个按钮 - 在这种情况下无需使用 LinearLayout。这增加了您的视图级别层次结构 - 这就是显示警告的原因。

您可以简单地使用 android:layout_below="@+id/Partfinder" 到您的第二个按钮来实现您想要的。

【讨论】:

    【解决方案2】:

    在 Eclipse Juno 中,您可以 Project 的 Properties -> Android Lint Preferences。现在,查找 UselessParent 并将其严重性设置为忽略或单击全部忽略。 但最后,您的问题是您的 RelativeLayout 只有一个孩子。你可以这样删除它:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="@drawable/GCBS_Template_BKGND"
        tools:context=".MainActivity" >
    
        <Button
            android:id="@+id/Partfinder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Part Finder" />
    
        <Button
            android:id="@+id/Heaterfinder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Heater Finder" />
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      根据您的 xml 布局创建,警告非常清楚。只需添加 2 个按钮,您可能不需要同时使用相对和线性布局,但只需其中一种布局就足够了。因此,通过删除 XML 中的线性布局应该可以帮助您进一步进行如下操作。

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="@dimen/activity_vertical_margin"
      android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:background="@drawable/GCBS_Template_BKGND"
      tools:context=".MainActivity" >
      
          <Button
              android:id="@+id/Partfinder"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Part Finder" />
      
          <Button
              android:id="@+id/Heaterfinder"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:text="Heater Finder" />
      </RelativeLayout>
      

      在此之后,只需将按钮拖放到 xml 的布局视图中,并将它们放置在您想要按钮的任何位置。这应该会将 XML 脚本自动放入您的 XML 文件中。

      【讨论】:

        猜你喜欢
        • 2012-05-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-11
        相关资源
        最近更新 更多