【问题标题】:How to change Layout background dynamically in Android?如何在Android中动态更改布局背景?
【发布时间】:2014-09-28 21:18:43
【问题描述】:

我在启动画面的时候解析了JSON,其中的图片url被解析为登录屏幕的背景图片。这是登录屏幕的示例XMLcode:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_bg"  <!-- I want to change this background dynamically. --> 
android:focusableInTouchMode="true"
android:gravity="center"
tools:context=".activity.LoginActivity" >

<ScrollView
    android:id="@+id/mainScrollView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

 <!-- .... Here is edit text for login inputs and buttuns for singnup and login. -->

    </LinearLayout>
</ScrollView>   
</RelativeLayout>

在上面我已经在RelativeLayout的背景中放置了静态图像,但我想根据图像url将背景设置为可变的。

提前致谢。

【问题讨论】:

    标签: android xml json layout imageurl


    【解决方案1】:

    你需要将url图片转换为位图,然后将位图图片转换为Drawable并设置为RelativeLayout。

    首先将url图片转换为位图,见示例代码。

    Bitmap myImage = getBitmapFromURL("http://looksok.files.wordpress.com/2011/12/me.jpg");
    

    参考RelativeLayout

    RelativeLayout rLayout=(RelativeLayout)findViewById(R.id.relativeLayout);
    

    BitmapDrawable(obj) 将 Bitmap 对象转换为可绘制对象。

    Drawable dr = new BitmapDrawable(myImage);
    rLayout.setBackgroundDrawable(dr);
    

    Url图片到位图的转换方法

     public Bitmap getBitmapFromURL(String imageUrl) {
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                Bitmap myBitmap = BitmapFactory.decodeStream(input);
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    

    【讨论】:

    • 它在 connection.connect() 处抛出异常;
    • 08-06 09:55:57.635: W/System.err(19973): android.os.NetworkOnMainThreadException
    • @user3751280 你需要使用AsyncTask
    • 另一个是:08-06 09:55:57.655: W/System.err(19973): java.io.IOException 08-06 09:55:57.656: W/System.err (19973): 在 libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:87) 08-06 09:55:57.656: W/System.err(19973): 在 np.com.autolife.activity.LoginActivity。 getBitmapFromURL(LoginActivity.java:275)
    • @user3751280 检查我的答案,它会帮助你。
    【解决方案2】:

    试试这个方法,希望能帮助你解决问题。

    1. here下载最新的AndroidQuery jar:

    2. 把这个 jar 放到你的 libs 文件夹中,然后右键单击 jar 和 Build Path -> Add to bulid path。

    3. 如何使用看这个例子:

      AQuery androidQuery = new AQuery(this);

      androidQuery.ajax(url.trim(), Bitmap.class, 0, new AjaxCallback<Bitmap>() {
           @Override
           public void callback(String url, Bitmap object, AjaxStatus status) {
               super.callback(url, object, status);
               yourRelativeLayout.setBackground(new BitmapDrawable(object));
           }
      });
      

    【讨论】:

    • jar文件的下载链接在哪里?
    • 请检查我已经添加了。
    【解决方案3】:

    我会这样做

    像这样调用 AsyncTask

     new GetImageFromServer().execute(strUrl);  // strUrl is your URL
    

    这里是 AsyncTask 类

    public class GetImageFromServer extends AsyncTask<String, Void, Bitmap>
        {
    
    
            private Bitmap image;
    
            protected void onPreExecute(){
                super.onPreExecute();
    
            }
    
            @Override
            protected Bitmap doInBackground(String... params){
                try{
    
                    URL urli = new URL(params[0].trim());
                    URLConnection ucon = urli.openConnection();
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;
    
    
                    image = BitmapFactory.decodeStream(ucon.getInputStream(),null,options);
    
                } catch (MalformedURLException e) {
    
                    e.printStackTrace();
                } catch (IOException e) {
    
                    e.printStackTrace();
                }
                return image;  //<<< return Bitmap
            }
            @Override
            protected void onPostExecute(Bitmap result){ 
    
                 RelativeLayout relative = (RelativeLayout) findViewById(R.id.loginLayout);
                 Drawable dr = new BitmapDrawable(result);
                 relative.setBackgroundDrawable(dr);
    
                }
    
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 1970-01-01
      • 2011-08-31
      • 2016-01-22
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多