Android Studio - 避免针对不同 API 的布局警告

Android Studio - avoid layout warning for different APIs

我在res/layout

中有这个
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/txt"
    android:layout_alignBottom="@+id/txt"
    android:layout_toRightOf="@+id/txt"
    android:text="@string/..."
    android:layout_marginLeft="3dp"
    android:textAppearance="?android:attr/textAppearanceSmall" />

我收到警告说 layout_toRightOf 应该是 layout_toEndOf 并且 layout_marginLeft 应该是 layout_marginStart 以更好地支持从右到左的布局。

所以我制作了 2 个布局并将另一个放在 layout-v17 中并更改了它想要的内容。虽然 layout-v17 现在可以了,但原始布局文件上的警告仍然存在,我如何让它知道我已经处理了?

这些警告来自 Lint,这是一个在您的 SDK 中实现的工具,它正在检查整个项目以寻找潜在的错误。 检查 this and this

这里有一个 list 的 Lint 检查,只要您在根视图中定义了 xmlns:tools="http://schemas.android.com/tools",就可以使用 tools:ignore= 属性忽略每个检查。 当然,此功能应仅在需要时使用,因为 lint 检查可用于防止错误。

与您的问题相关的属性是 RtlHardcoded

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="RtlHardcoded" >

    <TextView
        android:text="@string/text"
        android:layout_toRightOf="@+id/txt" />
</RelativeLayout>

请注意,您还可以从整个项目的 IDE 选项中禁用特定的 Lint 检查,但我不会那样做。 (在 Android Studio 中,项目设置 -> 检查)。

编辑: 另请注意,这并没有忽略一个真正的问题。我建议你这样做是因为我想象你只是 "bothered" 收到关于你已经处理的情况的警告。在这些情况下,tools:ignore 可以用来告诉 Lint "OK, I'm aware of the issue and I already managed it"(大多数情况下,Lint 足够聪明,可以自己注意到)。

关于从右到左的布局,最好的处理方法是同时放置 left(right) / start(end) 属性。 API>17,后者优先。

放两次 layout_toRightOf 和 layout_toEndOf 在同一个文本视图中

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/txt"
    android:layout_toRightOf="@+id/txt"
    />

试试这个

<TextView
android:id="+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBaseline="id/txt" 
android:layout_alignBottom="id/txt" 
android:layout_toRightOf="id/txt" 
android:text="@string/..." 
android:layout_marginLeft="3dp" 
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
android:id="+id/textView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBaseline="txt" 
android:layout_alignBottom="txt" 
android:layout_toRightOf="txt" 
android:text="@string/..." 
android:layout_marginLeft="3dp" 
android:textAppearance="?android:attr/textAppearanceSmall" />