Android 如何为背景图像添加填充
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2711309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to add padding for background image
提问by michael
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
背景图像(一个 9 补丁的 png 文件)。如何向左右添加填充以使背景图像不占用整个宽度?我试过android:paddingLeft
and android:paddingRight
,但这不会改变任何东西。
<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.
整个背景仍然拉伸整个屏幕宽度。
采纳答案by stealthcopter
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
. 通过LinearLayout
在这个里面使用第二个填充将生效。您必须定义LinearLayout
在填充区域中可见的第一个背景颜色。
<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>
Note: This is probably also possible by using an XML file for the background.
注意:这也可能通过使用 XML 文件作为背景来实现。
回答by Vasily Sochinsky
You should use xml drawable, specially designed for this purposes - Inset Drawable: http://developer.android.com/guide/topics/resources/drawable-resource.html#Inset
您应该使用专门为此目的设计的 xml drawable - Inset Drawable:http: //developer.android.com/guide/topics/resources/drawable-resource.html#Inset
For example:
例如:
res/drawable/inset_background.xml:
res/drawable/inset_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/your_background_image"
android:insetRight="25dip"
android:insetLeft="25dip" />
and then in your xml layout:
然后在您的 xml 布局中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/inset_background">
回答by xihua
Ur background drawable should be like this,using android:bottom
你的背景drawable应该是这样的,使用android:bottom
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:bottom="48dp" android:drawable="@drawable/bg">
</item>
</layer-list>
回答by Mo1989
You can just use
你可以使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dip"
android:layout_marginRight="25dip"
android:background="@drawable/background">