This RelativeLayout layout or its LinearLayout par

2019-01-25 09:15发布

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

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

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

downloadFormsButton and formErrorMsg must be always in form.

But using that xml I get this warning:

This RelativeLayout layout or its LinearLayout parent is useless

I need linearLayout to add TableLayouts programmatically.

How can I solve this warning?

4条回答
我想做一个坏孩纸
2楼-- · 2019-01-25 09:53

Lint is correct in it's warning, your LinearLayout isn't doing anything useful, since its only child is a RelativeLayout. Remove the 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>
查看更多
仙女界的扛把子
3楼-- · 2019-01-25 10:02

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

EDIT:

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

查看更多
我命由我不由天
4楼-- · 2019-01-25 10:06

That is just an warning from lint. Lint doesn'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).

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

查看更多
戒情不戒烟
5楼-- · 2019-01-25 10:19

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>
查看更多
登录 后发表回答