【问题标题】:"This RadioGroup layout or its RelativeLayout parent is possibly useless"“此 RadioGroup 布局或其 RelativeLayout 父级可能没用”
【发布时间】:2014-03-07 18:19:28
【问题描述】:

我有带有 RadioGroup 的 RelativeLayout 视图。我正在尝试将 RadioGroup 嵌入到 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"
tools:context=".MainActivity" >

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
>
    <RadioButton 
        android:id="@+id/r1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Example1"
        android:checked="true"
    />

    <RadioButton 
        android:id="@+id/r2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Example2"    
        android:checked="true"
    />
</RadioGroup>

这样做会显示异常:

This RadioGroup layout or its RelativeLayout parent is possibly useless

我该如何解决这个警告?

【问题讨论】:

  • RadioGroup 是您的RelativeLayout 中唯一的东西吗?如果是这样,那将是无用的。此外,这只是一个警告,因此可以忽略并继续运行。

标签: android xml android-layout adt


【解决方案1】:

通常,当您的布局中只有一个元素时会显示此警告。

例如,一个 LinearLayour 里面只有一个 LinearLayour(最后一个可能有很多 Views)是没有用的,因为它不用于任何布局目的(布局的目的是分发视图)。

在您的具体情况下,您可以去掉外部的RelativeLayout

【讨论】:

    【解决方案2】:

    是的,你是对的,这只是减少视图层次结构的警告。

    如果您的布局只有 Single RadioGroup,请使用以下代码

    <RadioGroup
    
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
        <RadioButton 
            android:id="@+id/r1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Example1"
            android:checked="true"
        />
    
        <RadioButton 
            android:id="@+id/r2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/Example2"    
            android:checked="true"
        />
    </RadioGroup>
    

    【讨论】:

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