布局不会排列以创建gridview样式

时间:2014-07-10 03:29:21

标签: java android android-layout layout android-linearlayout

我试图创建一种gridview,它需要根据其他一些要求进行定制,所以请耐心等待。

我有一个scrollview,在scrollview中是一个linearlayout 我正在使用FetchableImageView类(这是一个具有远程文件支持和缓存的ImageView) 我知道计算的数字是正确的(从查看调试信息) 虽然不知何故,图像被推下并且不允许其他图像彼此相邻放置。

我如何调整这些,我也尝试过RelativeLayout,但它根本没有做任何事情。 for循环将来自String输入,我仍然试图弄清楚布局,这就是为什么它在一个普通的for循环中。 (ps:不要介意图片网址。我很快就抓住了占位符文件,但现在意识到图片的内容......)

/************************************
 * 
 ************************************/
private void processJsonStringAndUpdateUI(String result)
{

    // Init menu
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.viewslayout);
    linearLayout.removeAllViews();
    linearLayout.setMinimumHeight(metrics.heightPixels);


    //
    // do some calculations about issues
    // we have measured that the image should be 70% of the background image
    // we have also measured that the image should be 10% from the bottom


    // 1. Lets get the background image size
    Drawable d = getResources().getDrawable(R.drawable.background);
    int h = d.getIntrinsicHeight(); 
    int w = d.getIntrinsicWidth();  

    // 2. Calculate 10% of HEIGHT
    int tenPercent = (int) (h*0.1);

    // 3. calculate the height of the image
    int imageHeight = (int) (h*0.7);

    // 4. calculate the image width which is 3:4 ratio
    int imageWidth = (int)(imageHeight/4) * 3;

    // 5. calculate how many we can fit per width (with some padding of course
    int padding = 20;
    int minimumImageArea = (int) imageWidth + (padding*2);
    int columns = (int) Math.floor(metrics.widthPixels / minimumImageArea);



    // 6. Offset and Multipliers
    int heightOffSet = (int) h - tenPercent - imageHeight;
    int widthOffSet = (int) ((w/columns) / 2) - (imageWidth/2);


    int heightMultiplier = h;
    int widthMultiplier = (int)(w/columns);




    int row = 0;
    int counter = 0;
    for(int x = 0; x < 4; x = x+1) {            
        FetchableImageView image = new FetchableImageView(this, null);            
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(imageWidth, imageHeight);        
        image.setImageResource(R.drawable.issue);
        image.setId(1);     

        params.setMargins(widthOffSet + (counter*widthMultiplier),heightOffSet + (row*heightMultiplier),0,0);

        linearLayout.addView(image, params);
        image.setImage("http://upload.wikimedia.org/wikipedia/en/2/22/Seventeen_Magazine_cover.jpg");

        counter++;
        if (counter == columns)
        {
            counter = 0;
            row++;
        }
    }


}

1 个答案:

答案 0 :(得分:0)

找到解决方案......

让父亲成为RelativeLayout

然后将relativeLayoutParams与setMargin一起应用于图像

FetchableImageView image = new FetchableImageView(this,null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(270,360);         image.setImageResource(R.drawable.issue);         params.setMargins(20,20,0,0);         relativeLayout.addView(image,params);         image.setImage(&#34; http://upload.wikimedia.org/wikipedia/en/2/22/Seventeen_Magazine_cover.jpg&#34);         image.setBackgroundColor(Color.RED);