Android布局在使用9-patch背景时出现问题。

36
我在将背景更新为9-patch图像时遇到了问题。使用同一图像的不同尺寸在不同屏幕上的布局很好,但是当我将图像更改为9-patch时,整个布局就神秘地崩溃了。
之前的布局如下: 原始布局 http://onik.org/android/layoutOk.png
更改为9-patch后: 9-Patch图像 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>

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

9
你能否在这里重新上传图片并将其作为嵌入式图片显示?你的网站目前缺少这些图片,这让这个问题变得不太易读。 - Brad Larson
哦,抱歉,我忘记了它们在这里链接。我会尝试看看家里是否有旧备份,但我认为它们已经丢失了... - onik
你救了我的一天。这很难调试。谢谢!!! - Matt W
3个回答

104

它的意思是只需添加属性

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文档中有详细解释:

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

要忽略此默认行为,我们需要在XML布局中明确设置填充属性。


谢谢 :) 但是你知道为什么这个有效吗?或者有什么错误吗? - Eric Nordvik
2
http://developer.android.com/guide/developing/tools/draw9patch.html 中的右侧和底部定义了9-patch图像内的填充。您应该在9-patch图像中或在xml布局中明确设置这些填充。 - vokilam
@Vokilam,我刚找到这个回复。如果将其用作布局容器的背景,该怎么办?我正在使用LinearLayout,但子视图没有尊重内容区域。它们一直到边缘。有什么想法吗? - karl
我如何在保持背景纵横比的情况下使用它?我需要为一个ViewPager使用它,该ViewPager仅在图像内显示其页面。这是关于它的线程:http://stackoverflow.com/q/31229007/878126 - android developer

3

问题已解决,我的9-patch填充区域不合适。当我将填充区域绘制为可拉伸区域的反向时,图片开始正常工作。我只是认为如果没有填充区域,这应该是自动的 :)


1
使用9-patch设置内容行(右侧和底部)!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接