【问题标题】:how to add Images next to each other in android如何在android中将图像彼此相邻添加
【发布时间】:2014-03-25 13:11:53
【问题描述】:

大家好,我有一个关于在我的布局中添加图像的问题。 现在我已经下载了四张图片,但是我所有的图片都堆叠在一起, 我希望能够让它们彼此相邻,例如顶部 2 张图片和底部 2 张图片,最好的方法是什么? 现在我正在使用相对布局,我使用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);
}
}

}

【问题讨论】:

  • 你读过android网站上的Linear和RelativeLayout吗?
  • 你应该使用gridview
  • 我认为 Nicko 的解决方案非常适合我的项目,因为我只需要在屏幕上显示 4 张图片,谢谢您的建议,我会定义的。查看视图 Page 和 gridview 以备将来使用。
  • 使用 GridView 并将其列设置为 2.. 然后一行中只显示两个图像。

标签: java android


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    您可以通过几种不同的方式执行此操作,例如,如果您只需要在屏幕上显示这四张图片,您可以使用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>
    

    看起来像这样:

    希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      查看寻呼机 View pager

      Grid View With Gallery

      【讨论】:

        猜你喜欢
        • 2013-04-22
        • 2017-12-26
        • 2014-09-03
        • 1970-01-01
        • 1970-01-01
        • 2013-06-17
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多