【问题标题】:What is an alternative to this XML layout (Lint Warning)此 XML 布局的替代方法是什么(Lint 警告)
【发布时间】:2012-07-03 03:17:48
【问题描述】:

好的,这个布局将为您提供 3 个宽度相同的图像,宽度与 3 个文本视图上最长的文本一样宽

它们的宽度相等,因为它们 match_parent 和 parent 是 wrap_content 到最大的 TextView。

3 个文本视图以背景为中心,左右相等。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000FF"
    android:gravity="center" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="#FF0000"
         android:drawableLeft="@drawable/ic_launcher"
         android:text="view 1"/>

      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="#FF0000"
         android:drawableLeft="@drawable/ic_launcher"
         android:text="view 2"/>

      <TextView
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:background="#FF0000"
         android:drawableLeft="@drawable/ic_launcher" 
         android:text="view 3 longer text"/>

  </LinearLayout>
</LinearLayout>

像这样:

问题是 Lint 警告说内部的 LinearLayout 是无用的。 (这不是因为它使内部的 textview 变得完全一样的宽度。

任何人都可以生成相同的布局但没有 lint 警告吗?

【问题讨论】:

  • 内部的 LinearLayout 包裹的是 textviews,允许较小的 TextViews 拉伸到最宽的 TextView 的大小。虽然不会拉伸到屏幕宽度
  • 避免 lint 警告的一个棘手方法是添加一个 textview 或另一个将其可见性设置为消失的小部件元素。

标签: android xml android-layout android-lint


【解决方案1】:

经过深思熟虑......这是我的解决方案,即将外部线性布局更改为框架布局

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000FF" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:drawableLeft="@drawable/ic_launcher"
            android:text="view 1" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:drawableLeft="@drawable/ic_launcher"
            android:text="view 2" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FF0000"
            android:drawableLeft="@drawable/ic_launcher"
            android:text="view 3 longer text" />
    </LinearLayout>

</FrameLayout>

您可以将线性布局上的布局重力设置为:

android:layout_gravity="center_vertical|center_horizontal"

或在框架布局上设置常规重力。

【讨论】:

    【解决方案2】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical">
    
      <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="view 1"/>
    
      <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="view 2"/>
    
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content" 
         android:text="view 3 longer text"/>
    </LinearLayout>
    

    这给了我类似的结果,而 Lint 没有任何错误。我已经从代码中删除了可绘制标签,因为我没有你的可绘制对象使用。只需添加它们。这里唯一的问题是您不能在 LinearLayout 标签中放置背景。您必须为您的活动创建一个自定义主题,这很容易,因为您可以将现有主题设置为父主题,然后只需更改背景值...更多关于主题here

    【讨论】:

    • 这是一个很好的解决方案,可以让您降低视图层次结构的复杂性。
    • 是的,问题在于是否值得为一个活动创建一个主题。也许我应该问我为什么要使用这个背景,我可以在其他地方使用它来扩展我的主题使用。所以说到底,我认为这就是答案。
    • @Blundell 您非常强调创建新主题。如果你真的写它,它只需要三行代码,最多五行。所以这不是太多的工作。
    • @udiboy 这不是代码行,而是为一个 Activity 创建一个独特的主题似乎不是正确的做法。因此,我重新评估了我的 Activity 以及我想要实现的目标,并创建了一个包含大多数应用程序的主题。这更像是。
    【解决方案3】:

    因为它只是一个警告,而且你知道布局不是无用的,你可以忽略它。

    或者,您可以通过将 android:background="@null" 添加到内部 LinearLayout 来欺骗 lint。

    【讨论】:

      【解决方案4】:

      您是否尝试过在线性布局中使用 RelativeLayout?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 2016-12-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多