Android 这个 RelativeLayout 布局或者它的 LinearLayout 父级是没用的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10319947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:17:35  来源:igfitidea点击:

This RelativeLayout layout or its LinearLayout parent is useless

androidandroid-layoutlayout

提问by VansFannel

I'm developing an Android 3.1. and above application.

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

On one activity I generate its layout dynamically. I'm using formdetail.xmlas contentview:

在一项活动中,我动态生成其布局。我使用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 :

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>

downloadFormsButtonand formErrorMsgmust be always in form.

downloadFormsButton并且formErrorMsg必须始终保持形式。

But using that xml I get this warning:

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

This RelativeLayout layout or its LinearLayout parent is useless

This RelativeLayout layout or its LinearLayout parent is useless

I need linearLayout to add TableLayouts programmatically.

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

How can I solve this warning?

我该如何解决这个警告?

采纳答案by DeeV

You either ignore it or right click your project. Click Build Path->Configure Build Path.... Under Android Lint Preferenceslook for UselessParentand set it's severity to ignore or click Ignore All.

您可以忽略它或右键单击您的项目。单击Build Path->Configure Build Path...。在Android Lint Preferences查找UselessParent并设置其严重性以忽略或单击Ignore All

EDIT:

编辑:

I take that last part back. I think Ignore Allwill wipe all Lint warnings and errors.

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

回答by CSmith

Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the LinearLayout:

Lint 的警告是正确的,你的 LinearLayout 没有做任何有用的事情,因为它唯一的孩子是一个 RelativeLayout。删除线性布局:

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

回答by Anuj TBE

I was also having the same issue with my app layout.

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

I don't know how and it is right way or not, but this get resolved after setting background attribute to both layouts.

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

Now there is no warning and working perfect. Hope it will also work for you.

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

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

回答by Luksprog

That is just an warning from lint. Lintdoesn't know you would later append views to this layout and it will warn you that you added an unnecessary extra view(increasing your layout depth).

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

You should just ignore the warning(If it really bothers you here is a link on how to remove warningsfrom lint).

您应该只是忽略警告(如果它真的困扰你这里是一个链接如何删除警告lint)。