如何在android中添加图片相邻的图片

如何在android中添加图片相邻的图片

问题描述:

嗨,我有一个关于添加图片到我的布局的问题。 现在我已经下载了四张照片,但是我的所有图像都是互相堆叠在一起的, 我希望能够使它们彼此相邻,例如顶部2张,底部2张,什么是最好的方法呢? 现在我正在使用相对布局,并使用AsyncTask来处理图像。如何在android中添加图片相邻的图片

我的代码:

public class MainActivity extends Activity { 

//String[] imgUrls = {getString(R.string.uncc_main_thumb),getString(R.string.football_main_thumb,getString(R.string.ifest_main_thumb),getString(R.string.commencement_main_thumb))}; 
String[] imgUrls={"http://farm5.staticflickr.com/4113/4843614620_c541de5a5c_m.jpg", 
        "http://farm8.staticflickr.com/7265/8151547298_85e60e7368_m.jpg", 
        "http://farm9.staticflickr.com/8054/8414075899_e87a74407b_m.jpg", 
        "http://farm9.staticflickr.com/8210/8277674769_7d1245dbf1_m.jpg"}; 
RelativeLayout root; 
ProgressDialog progressDialog; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    root = (RelativeLayout) findViewById(R.id.RelativeLayout1); 

    for (String imgUrl : imgUrls) { 
     new DownLoadImages().execute(imgUrl); 
    } 
} 

private class DownLoadImages extends 
AsyncTask<String, Void, Bitmap> { 
    @Override 
     protected Bitmap doInBackground(String... params) { 
     String imgUrl = params[0]; 
     Bitmap image = null; 
     try { 
      URL url = new URL(imgUrl); 
      image = BitmapFactory.decodeStream(url.openStream()); 
      } catch (MalformedURLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     return image; 
} 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      // show the loading message while downloading the image 
      progressDialog = ProgressDialog.show(MainActivity.this,null, "loading..."); 
     } 

     @Override 
     protected void onPostExecute(Bitmap result) { 
      if (result == null) { 
       result = ((BitmapDrawable) getResources().getDrawable(
         R.drawable.not_found)).getBitmap(); 
      } 

      ImageView imageViewToBeAdded = new ImageView(getBaseContext()); 
      imageViewToBeAdded.setLayoutParams(new LayoutParams(
        LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); 
      imageViewToBeAdded.setPadding(5, 5, 5, 5); 
      imageViewToBeAdded.setImageBitmap(result); 
      root.addView(imageViewToBeAdded); 
} 
} 

} 
+1

你看了关于线性和RelativeLayout的android上的网站? – helleye

+0

使用视图寻呼机。 –

+0

您应该使用gridview –

,如果你只需要这四个画面在你的屏幕上,你可以使用layout_weight属性视图为此,您可以在几种不同的方式,例如。 例如

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#aaaaaa" 
       android:gravity="center_vertical" 
       android:orientation="vertical"> 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:orientation="horizontal" 
      > 
     <ImageView 
       android:layout_weight="1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       android:src="@drawable/top_blue"/> 
     <ImageView 
       android:layout_weight="1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       android:src="@drawable/top_blue"/> 
    </LinearLayout> 
    <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal" 
      android:layout_weight="1" 
      > 
     <ImageView 
       android:layout_weight="1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       android:src="@drawable/top_blue"/> 
     <ImageView 
       android:layout_weight="1" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 

       android:src="@drawable/top_blue"/> 
    </LinearLayout> 


</LinearLayout> 

这应该是这样的: And that would look like this:

希望这有助于:)

+0

非常感谢我正在寻找的东西! – Samson

+0

这是我的荣幸:) –

最简单的方法是使用TableLayout 2行。

查看传呼机 View pager

Grid View With Gallery