Android 9-Patch扩展问题

时间:2013-01-14 17:38:17

标签: android background scale nine-patch

使用9-patch图像作为EditText字段的背景时,我遇到了缩放问题。我有一个伸展整个宽度的桌子,在这里我有下面的桌子,它填满了桌子的宽度。

问题是我使用9补丁图像作为背景,但编辑文本字段没有拉伸到整个宽度,它只延伸到提示文本的宽度。

<TableRow
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal" >

    <EditText
        android:id="@+id/et_username"
        android:contentDescription="@string/hint_enterUsername"
        android:hint="@string/hint_enterUsername"
        android:singleLine="true"
        android:textSize="@dimen/edittext_textsize"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"                    
        android:gravity="center_horizontal|center_vertical"
        android:background="@drawable/background_field_top" />

</TableRow>

我可以在图形视图中看到行的宽度确实拉伸但EditText字段没有,如附图所示

Graphical View

有没有人想过如何将edittext拉伸到整个尺寸?以下是我正在使用的9补丁图像。

9-patch image

3 个答案:

答案 0 :(得分:2)

来自TableRow

上的Android开发者文档
  

水平排列其子项的布局。 TableRow应该   始终用作TableLayout的子级。如果TableRow的父级是   不是TableLayout,TableRow将表现为水平   的LinearLayout。

@Nipun Gogia答案不正确,为什么在只有一个 EditText子项的LinearLayout中放置一个TableRow(表现得像Horizo​​ntal LinearLayout)?

您应该将EditText视图作为TableLayout的直接子项放置,然后它将显示为跨越所有表列的单行。如果需要,您可以添加更多TableRow和Columns。

使用Nine-Patch拉伸EditText视图的背景图像:

你需要编辑9.png文件的9补丁规则设置图像的可伸缩区域,将顶部规则(黑色1px线)放在图像的中间,现在它只是在透明处拉伸结束让图像拉长。 了解她的方法:http://developer.android.com/tools/help/draw9patch.html

Importent!记得用扩展名.9.png

保存图片

使用文件扩展名保存的九补丁图片:background。 9 .png

enter image description here


修改了较小的图像,由于九个补丁规则,它看起来与上面相同。

modified image


修改后的图像,但具有不可拉伸的高度。

enter image description here


注意:如果使用“将图像另存为...”下载上面的图像,请记住将 .9.png 添加到文件名中以使其正常工作。 < / p>

答案 1 :(得分:1)

确保您的图像文件名是background_field_top.9.png(确保.9。在那里) 我对你的图像做了一些小改动,这样圆角变圆更平滑,但这不是核心问题。 suggsted image 如果这没有帮助,请检查背景是否在没有表格的情况下正常工作,以隔离问题是布局还是背景图像。

答案 2 :(得分:1)

你的工作正常。我正在使用我自己的9补丁......这是我的代码:

        <LinearLayout
            android:id="@+id/layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:gravity="center_vertical|center_horizontal" >

                <EditText
                    android:id="@+id/et_username"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:background="@drawable/areanumber"
                    android:gravity="center_horizontal|center_vertical"
                    android:singleLine="true"
                    android:text="hello" />
            </TableRow>
        </LinearLayout>

这里areanumber是我的9补丁图片...只是尝试将你的代码包装在linearlayout中 我实现了你的9补丁图像,它也工作正常

enter image description here

相关问题