2014-03-25 4 views
1

안녕하세요 여러분, 레이아웃에 이미지를 추가하는 것에 대해 질문이 있습니다. 지금은 네 장의 사진을 다운로드 받았지만 모든 이미지는 서로 쌓아 놓았습니다. 두 장의 사진을 맨 아래에 그리고 두 장을 맨 아래에 놓고 서로 가깝게 만들 수있게하고 싶습니다. 이것에 접근 하는가? 지금 나는 상대적 레이아웃을 사용하고 있으며 이미지를 처리하기 위해 AsyncTask을 사용합니다.안드로이드에서 이미지를 서로 옆에 추가하는 방법

내 코드 :

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의에 대해 읽게하는 데 도움이? – helleye

+0

보기 호출기를 사용하십시오. –

+0

그리드 뷰 –

답변

0

당신이 당신의보기를 layout_weight 속성을 사용할 수 있습니다 화면에있는 것으로 만 4 개 그림을해야하는 경우 예를 들어, 여러 가지 방법으로이 작업을 수행 할 수 있습니다 Grid View With Gallery. 예를

<?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

그것은 나의 기쁨이었다 :) –

1

가장 쉬운 방법은 TableLayout 2 행을 사용하는 것입니다.

관련 문제