0

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

它们的宽度相等,因为它们 match_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 是无用的。(这不是因为它使内部文本视图变得完全相同的宽度。

皮棉错误

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

4

4 回答 4

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 标签中放置背景。您必须为您的活动创建一个自定义主题,这很容易,因为您可以将现有主题设置为父主题,然后只需更改背景值...更多关于主题的信息在这里

于 2012-07-02T17:49:10.383 回答
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"

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

于 2012-07-02T17:27:49.077 回答
1

由于这只是一个警告,并且您知道布局并非无用,因此您可以忽略它。

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

于 2012-07-02T17:50:41.897 回答
0

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

于 2012-07-02T17:59:28.910 回答