在网格样式的社交馈送上显示多个图像

时间:2014-09-29 08:20:51

标签: android android-layout

我正在尝试在我的Android应用的社交Feed列表视图中显示多个图片,类似于Facebook Android应用的功能。 请参考下图:

enter image description here

帖子中显示的最大图像数为3。 对于多个图像,我希望图像按照其大小以网格类型格式动态排列/调整大小,如图所示。

我尝试通过指定layout_weight来调整LinearLayout中的3个ImageView但不起作用。 任何形式的帮助/建议或示例代码都非常感谢!!! ORZ

1 个答案:

答案 0 :(得分:3)

我已经这样做了,请检查可能是你得到了答案  这是XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    >
    <FrameLayout
        android:id="@+id/imgframe"
        android:layout_width="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_height="match_parent">
    </FrameLayout>
    </RelativeLayout>

现在我已经以编程方式完成了图像的设置

public class ImageShow extends Activity {
            ImageView img1,img2,img3,img4;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.image_dsgn);
                Display display = getWindowManager().getDefaultDisplay();
                Point size = new Point();
                display.getSize(size);
                int width = size.x;
                int sst=width-20;
                Log.d("Width is",String.valueOf(sst));
                Log.d("Width is",String.valueOf(width));
                int i=3;
                FrameLayout frameLayout=(FrameLayout)findViewById(R.id.imgframe);
                if(i==1)
                {    ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.img3);
                    imageView.setPadding(0,5,0,0);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView.setLayoutParams(new FrameLayout.LayoutParams(width,400));
                    frameLayout.addView(imageView);
                }
                if(i==2)
                {    ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.img3);
                    imageView.setPadding(0,5,0,0);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView.setLayoutParams(new FrameLayout.LayoutParams(width/2,
                            400));
                    ImageView imageView1 = new ImageView(this);
                    imageView1.setImageResource(R.drawable.img3);
                    imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView1.setLayoutParams(new FrameLayout.LayoutParams(width/2,
                            400));
                    imageView1.setX(width/2);
                    imageView1.setPadding(5,5,0,0);
                    frameLayout.addView(imageView);
                    frameLayout.addView(imageView1);
                }
                if(i==3)
                {
                    ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.img3);
                    imageView.setPadding(0,5,0,0);
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView.setLayoutParams(new FrameLayout.LayoutParams(width/2,
                            400));
                    ImageView imageView1 = new ImageView(this);
                    imageView1.setImageResource(R.drawable.img3);
                    imageView1.setX(width/2);
                    imageView1.setPadding(5,5,0,0);
                    imageView1.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView1.setLayoutParams(new FrameLayout.LayoutParams(width/2,
                            200));
                    ImageView imageView2 = new ImageView(this);
                    imageView2.setImageResource(R.drawable.img3);
                    imageView2.setX(width/2);
                    imageView2.setY(200);
                    imageView2.setPadding(5,5,0,0);
                    imageView2.setScaleType(ImageView.ScaleType.FIT_XY);
                    imageView2.setLayoutParams(new FrameLayout.LayoutParams(width/2,
                            200));
                    frameLayout.addView(imageView);
                    frameLayout.addView(imageView1);
                    frameLayout.addView(imageView2);
                }
                if(i==4)
                {
                    //x=0,y=0
                    ImageView imageView = new ImageView(this);
                    imageView.setImageResource(R.drawable.img3);
                    imageView.setPadding(0,5,0,0);
                    imageView.setLayoutParams(new FrameLayout.LayoutParams(sst/2,400));
                    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                    //x=200,y==0
                    ImageView imageView1 = new ImageView(this);
                    imageView1.setImageResource(R.drawable.img3);
                    imageView1.setX(sst/2);
                    imageView1.setPadding(2,5,0,0);
                    imageView1.setLayoutParams(new FrameLayout.LayoutParams(sst/2,400/3));
                    imageView1.setScaleType(ImageView.ScaleType.FIT_XY);

                    ImageView imageView2 = new ImageView(this);
                    imageView2.setImageResource(R.drawable.img3);
                    imageView2.setX(sst/2);
                    imageView2.setY(400/3);
                    imageView2.setPadding(2,5,0,0);
                    imageView2.setLayoutParams(new FrameLayout.LayoutParams(sst/2,400/3));
                    imageView2.setScaleType(ImageView.ScaleType.FIT_XY);

                    ImageView imageView3= new ImageView(this);
                    imageView3.setImageResource(R.drawable.img3);
                    imageView3.setX(sst/2);
                    imageView3.setY((400/3+400/3));
                    imageView3.setPadding(2,5,0,0);
                    imageView3.setLayoutParams(new FrameLayout.LayoutParams(sst/2,400/3));
                    imageView3.setScaleType(ImageView.ScaleType.FIT_XY);


                    TextView textView=new TextView(this);
                    textView.setText("+ 6");
                    textView.setX(sst/2);
                    textView.setTextColor(Color.parseColor("#ffffff"));
                    textView.setTypeface(null, Typeface.BOLD);
                    textView.setY(400/3+400/3);
                    textView.setGravity(Gravity.CENTER);
                    textView.setTextSize(20);
                    textView.setLayoutParams(new FrameLayout.LayoutParams(sst/2,400/3));
                    frameLayout.addView(imageView);
                    frameLayout.addView(imageView1);
                    frameLayout.addView(imageView2);
                    frameLayout.addView(imageView3);
                    frameLayout.addView(textView);
                }

            }
        }