我想设置通过我的应用程序保存在SD卡上的主要布局的背景图像 [英] I want to set an image saved on sd card in main layout background through my application

查看:117
本文介绍了我想设置通过我的应用程序保存在SD卡上的主要布局的背景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造的,我想设置在主XML linearlayout.I不同的背景图片都保存在SD卡上5个图像文件。现在我要选择一个事先知情同意,并设置为我的残废XML的LinearLayout background.so它的应用程序将取代previous形象,展示新形象作为背景。

i am creating an application in which i want set different background images in main xml linearlayout.I have stored 5 image files on sd card .now i want to select a pic and set it as my maim xml linearlayout background.so it will replace the previous image and display the new image as background.

推荐答案

首先一个id分配到主的xml的LinearLayout,例如在下面的情况下,它被命名为容器

First assign an id to the main xml linearlayout, for example in the following case it is named" container"

    <!-- main.xml -->
<?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:id="@+id/container">
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
</LinearLayout>

然后在的.java code,你可以找到的布局对象,并设置一个绘制为背景:

Then in the .java code you can find the layout object and set a drawable as its background:

package org.example.app;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String pathName = "/sdcard/gif001.gif";
        Resources res = getResources();
        Bitmap bitmap = BitmapFactory.decodeFile(pathName);
        BitmapDrawable bd = new BitmapDrawable(res, bitmap);
        View view = findViewById(R.id.container);
        view.setBackgroundDrawable(bd);
    }
}

问候

紫藤陈

这篇关于我想设置通过我的应用程序保存在SD卡上的主要布局的背景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆