如何为背景图片添加填充

如何为背景图片添加填充

本文介绍了如何为背景图片添加填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 LinearLayout ,其中有一个背景图片(一个9个补丁的png文件).如何在左右两侧添加填充,以使背景图像不会占据整个宽度?我已经尝试过 android:paddingLeft android:paddingRight ,但这并不会改变任何内容.

I have a LinearLayout which has a background image (a 9 patched png file).How can I add padding to left and right so that the background image does not take up the whole width? I have tried android:paddingLeft and android:paddingRight, but that does not change anything.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="25dip"
    android:paddingRight="25dip"
    android:background="@drawable/background">

整个背景仍然会拉伸整个屏幕宽度.

The whole background still stretches the whole screen width.

推荐答案

这无效,因为填充仅作用于 LinearLayout 的内容.通过在其中使用第二个 LinearLayout ,填充将生效.您必须定义第一个 LinearLayout 的背景颜色,该颜色将在填充区域中可见.

That doesn't work because padding only acts on the contents of the LinearLayout. By using a second LinearLayout inside this one the padding will take effect. You must define the background color of the first LinearLayout that will be visible in the padding area.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="25dip"
    android:paddingRight="25dip"
    android:background="#FF000000">

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">

    </LinearLayout>

</LinearLayout>

注意:这可能也可以通过使用XML文件作为背景来实现.

Note: This is probably also possible by using an XML file for the background.

这篇关于如何为背景图片添加填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:04