【问题标题】:Android layout broken with 9-patch backgroundAndroid 布局被 9-patch 背景破坏
【发布时间】:2010-10-11 09:09:08
【问题描述】:

我在更新背景以使用 9-patch 图像时遇到了这个问题。使用相同图像的不同尺寸的不同屏幕上的布局很好,但是当我将图像更改为 9-patch 时,它神秘地破坏了整个布局。

之前的布局是这样的:

Original layout http://onik.org/android/layoutOk.png.

当改为 9-patch 时:

9-Patch image http://onik.org/android/layoutError.png.

XML 文件保持不变,只是更改了 PNG 文件。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg">

    <TextView 
        android:text="@string/title"
        android:id="@+id/login_title"
        android:layout_width="fill_parent"
        android:layout_height="40px"
        android:textSize="30px">
    </TextView>

    <View
        android:id="@+id/login_underline"
        android:layout_width="wrap_content"
        android:layout_height="2px"
        android:background="#aaaaaa">
    </View>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:stretchColumns="1"
        android:gravity="center"
        android:paddingLeft="10px"
        android:paddingRight="10px">

        <TableRow>
            <TextView
                android:id="@+id/login_usernameLabel"
                android:text="@string/login_usernameLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="5px">
            </TextView>

            <EditText
                android:text=""
                android:id="@+id/login_username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:inputType="textEmailAddress"
                android:lines="1"
                android:nextFocusDown="@+id/login_password">
            </EditText>

        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/login_passwordLabel"
                android:text="@string/login_passwordLabel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </TextView>

            <EditText
                android:text=""
                android:id="@+id/login_password"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:nextFocusDown="@+id/login_login"
                android:inputType="textPassword">
            </EditText>

        </TableRow>

        <TableRow android:gravity="right">

            <Button
                android:text="@string/login_login"
                android:id="@+id/login_login"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">
            </Button>   


     </TableRow>

        <TableRow>

            <CheckBox
                android:id="@+id/login_remember"
                android:text="@string/login_remember"
                android:layout_span="2">
            </CheckBox>

        </TableRow>

 </TableLayout>


</LinearLayout>

有什么想法吗?或者其他获得非缩放、居中背景的解决方案?

【问题讨论】:

  • 你能把这里的图片重新上传为内嵌图片吗?您的网站目前缺少它们,这使这个问题的可读性降低了一些。
  • 哦,对不起,我忘了他们是链接在这里的。我会试着看看我家里是否有旧的备份,但我认为它们已经不见了......
  • 你拯救了我的一天。这很难调试。谢谢!!!

标签: android android-layout


【解决方案1】:

这意味着只是添加属性

android:padding="0dip"

到你的布局标签

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:padding="0dip">

更新:

Android Docs有确切的解释:

"您还可以通过在右侧和底部绘制一条线来定义图像的可选可绘制部分(实际上是填充线)。如果 View 对象将 NinePatch 设置为其背景,然后指定 View 的文本,它会自行拉伸,以使所有文本仅适合由右边和底线指定的区域(如果包括)。如果不包括填充线,Android 使用左边和顶线来定义这个可绘制区域。

要忽略这个默认行为,我们需要在 XML 布局中显式设置填充属性

【讨论】:

  • 谢谢 :) 但是你知道为什么会这样吗?或者是什么错误?
  • developer.android.com/guide/developing/tools/draw9patch.html 右侧和底部定义了 9-patch 图像内的填充。您应该在 9-patch 图像中或在 xml 布局中明确设置这些填充
  • @Vokilam,我刚刚找到了这个回复。将它用作布局容器的背景怎么样?我将它用于 LinearLayout,但子视图不尊重内容区域。他们一直走到边缘。有什么想法吗?
  • 如何在使用背景的同时保持其纵横比?我需要它用于仅在图像内显示其页面的 ViewPager。这是关于它的线程:stackoverflow.com/q/31229007/878126
  • 非常感谢。你救了我的命
【解决方案2】:

解决了问题,我在 9-patch 上的填充区域不合适。当我将填充区域绘制为可拉伸区域的倒数时,图像开始起作用。我只是认为如果没有填充区域,这将是自动的:)

【讨论】:

    【解决方案3】:

    使用 9-patch 设置内容行(右下角)!

    【讨论】:

      猜你喜欢
      • 2012-09-02
      • 2017-11-25
      • 1970-01-01
      • 2018-03-19
      • 1970-01-01
      • 2012-04-05
      • 1970-01-01
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多