简体   繁体   中英

How to Set Layout Background in Android UI

I'm new to Android programming. I have a UI with some TextView and Button controls. How do I set a background behind those components? Lets call it background.png .

在你的父布局元素中,例如linearlayout或者其他,只需添加android:background="@drawable/background"这将设置你的布局的背景,假设你有一个/ drawable文件夹中的图像。

*its very simple past a image in res/drawable-ldpi/ folder whatever you want to diaplay and in your xml write this.

< LinearLayout 

    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/myview"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="top"
    android:background="@drawable/icon">
< Button/>

< TextView   />


< / LinearLayout>

First you have to place your background.png image in your res/drawable/ folder.Later you have to set a parent layout for your TextView and Button widgets.I will consider a LinearLayout as a parent layout for you and the code goes like this:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="@drawable/background.png" 
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
 <TextView    android:layout_width="fill_parent"
              android:layout_height="wrap_content" 
              android:text="@string/hello" />
 <Button      android:text="Button" 
              android:id="@+id/button1"
              android:layout_width="wrap_content" 
              android:layout_height="wrap_content"></Button>
</LinearLayout>

Have a look at : android:background

This is a sample xml of what you might need to build on ,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/my_view"
    xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"    
    android:layout_height="fill_parent"    
    android:background="@drawable/background">        
</FrameLayout> 

You can set background color/drawable in xml file or from java file.

  1. Changing background in XML file

    android:background is responsible for setting up background in View. So add this if you want to set background to any View . Eg. in top LinearLayout , RelativeLayout or TextView or Button etc. Depends on your requirements.

    android:background="@drawable/background"

  2. Changing background from Java file

    use setBackgroundResource to set background to any View.

    or simply if you want to change the color of the View you have to use setBackgroundColor

first right click on "Drawable" folder in your Project Tab. select new-> image asset and then select your image.

In your layout coding of xml file, set android:background="@Drawable/background.png"

first copy high resolution image that you want to set in background and then past it(Doing right click on Drawable folder).

then set property of layout as below android:background="@drawable/Yourimagefilename"

您还可以通过以下方式以编程方式添加背景图像:

getWindow().setBackgroundDrawableResource(R.drawable.your_image);

android:background="@drawable/yourBG" in XML.

yourLayout.setBackgroundResource(resid);

or yourLayout.setBackgroundDrawable(drawble);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM