【问题标题】:This RelativeLayout layout or its LinearLayout parent is useless这个 RelativeLayout 布局或者它的 LinearLayout 父级是没用的
【发布时间】:2012-05-06 09:22:18
【问题描述】:

我正在开发 Android 3.1。及以上应用。

在一项活动中,我动态生成其布局。我使用 formdetail.xml 作为内容视图:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.formdetail);

    Bundle extras = getIntent().getExtras();
    if (extras != null)
    {
        currentFormId = extras.getString("FormId");
    }
}

formdetail.xml:

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

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/downloadFormsButton"
            android:enabled="false"
            android:layout_alignParentLeft="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="@string/download_forms_button" />
        <TextView
            android:id="@+id/formErrorMsg"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="10dp"
            android:textSize="16dp" >
        </TextView>
    </RelativeLayout>

</LinearLayout>

downloadFormsButtonformErrorMsg 必须始终保持格式。

但是使用那个 xml 我得到了这个警告:

This RelativeLayout layout or its LinearLayout parent is useless

我需要 linearLayout 以编程方式添加 TableLayouts。

我该如何解决这个警告?

【问题讨论】:

  • 我认为你的父布局只包含一个孩子,你可以通过删除父 LinearLayout 来优化你的布局

标签: android android-layout layout


【解决方案1】:

Lint 的警告是正确的,您的 LinearLayout 没有做任何有用的事情,因为它唯一的孩子是 RelativeLayout。移除 LinearLayout:

<?xml version="1.0" encoding="utf-8"?> 

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/detailLayout"
 android:layout_width="match_parent"
 android:layout_height="match_parent">

 <Button android:id="@+id/downloadFormsButton" 
         android:enabled="false"
         android:layout_alignParentLeft="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:layout_margin="10dp"
         android:text="@string/download_forms_button" />
 <TextView 
         android:id="@+id/formErrorMsg" 
         android:layout_alignParentRight="true"
         android:layout_alignParentBottom="true"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:padding="10dp"
         android:textSize="16dp" />
</RelativeLayout>

【讨论】:

  • 正如您在我的问题中看到的那样,我需要一个 LinearLayout 来以编程方式添加 TableLayouts。
  • 那么您可以将它们添加到您的 RelativeLayout ...无论哪种方式,我都只是在解释 Lint 警告的原因
【解决方案2】:

您要么忽略它,要么右键单击您的项目。点击Build Path-&gt;Configure Build Path...。在Android Lint Preferences 下查找UselessParent 并将其严重性设置为忽略或单击Ignore All

编辑:

我收回最后一部分。我认为Ignore All 将清除所有 Lint 警告和错误。

【讨论】:

  • 在 Eclipse Juno 中,此菜单位于项目的 Properties->Android Lint Preferences 中
【解决方案3】:

我的应用布局也遇到了同样的问题。

我不知道它是如何正确的,但是在为两个布局设置背景属性后,这个问题得到了解决。

现在没有警告并且工作完美。希望它也对你有用。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:background="@drawable/bg"
    android:layout_height="match_parent" >

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:background="@drawable/bg2"
            android:layout_marginTop="10dp" >

            <ImageView
                android:id="@+id/img_BookImage"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:scaleType="fitXY"
                android:contentDescription="@string/India"
                android:src="@drawable/india" />
        </FrameLayout>

    </FrameLayout>

【讨论】:

  • 我有两个被 linter 识别为多余的线性布局,但第一个布局包含一个固定到屏幕原点的背景图像,第二个布局将一些输入字段和文本水平居中放置在一个组中和垂直。如果不使用这两种布局,我找不到一种方法,因此定义两种布局的 android:background 属性消除了警告。
【解决方案4】:

这只是来自lint 的警告。 Lint 不知道您稍后会在此布局中添加视图,它会警告您添加了不必要的额外视图(增加了布局深度)。

你应该忽略这个警告(如果它真的困扰你,这里有一个来自how to remove warnings 的链接,来自lint)。

【讨论】:

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