此RelativeLayout布局或其LinearLayout父级是无用的

Van*_*nel 11 layout android android-layout

我正在开发Android 3.1.以上申请.

在一个活动中,我动态生成其布局.我使用formdetail.xml作为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");
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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

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

This RelativeLayout layout or its LinearLayout parent is useless

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

我该如何解决这个警告?

CSm*_*ith 14

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>
Run Code Online (Sandbox Code Playgroud)

  • 然后你可以将它们添加到RelativeLayout ......无论哪种方式,我只是解释了Lint警告的原因 (2认同)

Dee*_*eeV 8

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

编辑:

我把最后一部分拿回来.我认为Ignore All会擦除所有Lint警告和错误.