Android布局打破了9补丁背景

oni*_*nik 36 android android-layout

我在更新背景以使用9补丁图像时遇到此问题.使用不同尺寸的相同图像在不同的屏幕上布局很好,但是当我将图像更改为9个补丁时,它会神秘地打破整个布局.

之前的布局如下所示:

原始布局http://onik.org/android/layoutOk.png.

当更改为9补丁时:

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>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?或其他解决方案,以获得非缩放,居中的背景?

vok*_*lam 101

它意味着只需添加属性

android:padding="0dip"
Run Code Online (Sandbox Code Playgroud)

到你的布局标签

<?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">
Run Code Online (Sandbox Code Playgroud)

更新:

Android Docs有确切的解释:

"你也可以通过在右边和底线上画一条线来定义图像的可选可绘制部分(实际上是填充线).如果View对象将NinePatch设置为背景,然后指定View的文本,它将拉伸本身,以便所有的文字只能由右侧和底部线(如果有)指定的区域内适合.如果不包括填充线,Android使用左侧和顶部线来定义这个可绘制区域. "

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

  • http://developer.android.com/guide/developing/tools/draw9patch.html右侧和底侧定义9补丁图像内的填充.您应该在9-patch图像中或在xml布局中显式设置这些填充 (2认同)