Java 布局赢得';t对齐以创建gridview样式

Java 布局赢得';t对齐以创建gridview样式,java,android,android-layout,layout,android-linearlayout,Java,Android,Android Layout,Layout,Android Linearlayout,我正在尝试创建一种gridview,由于其他一些要求,它需要定制,所以请耐心等待 我有一个滚动视图,滚动视图内部是一个线性布局 我使用的是FetchableImageView类(这是一个具有远程文件支持和缓存的ImageView) 我知道计算的数字是正确的(通过查看调试信息) 尽管图像被向下推,不允许另一个图像彼此相邻 如何对齐这些,我也尝试过RelativeLayout,但它什么都没做。 for循环将来自字符串输入,我仍在尝试找出布局,这就是为什么它位于普通for循环中。 (注:不要介意图片的

我正在尝试创建一种gridview,由于其他一些要求,它需要定制,所以请耐心等待

我有一个滚动视图,滚动视图内部是一个线性布局 我使用的是FetchableImageView类(这是一个具有远程文件支持和缓存的ImageView) 我知道计算的数字是正确的(通过查看调试信息) 尽管图像被向下推,不允许另一个图像彼此相邻

如何对齐这些,我也尝试过RelativeLayout,但它什么都没做。 for循环将来自字符串输入,我仍在尝试找出布局,这就是为什么它位于普通for循环中。 (注:不要介意图片的url。我很快抓到了一个占位符文件,但现在意识到图片是关于什么的…)

/************************************
* 
************************************/
私有void进程JSONSTRINGANDUPDATEUI(字符串结果)
{
//初始化菜单
DisplayMetrics=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(度量);
LinearLayout LinearLayout=(LinearLayout)findViewById(R.id.viewslayout);
linearLayout.removeAllViews();
linearLayout.setMinimumHeight(度量.高度像素);
//
//对问题做一些计算
//我们已经测量过,图像应该是背景图像的70%
//我们还测量了图像距离底部的10%
//1.让我们获取背景图像的大小
Drawable d=getResources().getDrawable(R.Drawable.background);
inth=d.getIntrinsicHeight();
int w=d.getIntrinsicWidth();
//2.计算高度的10%
整数百分之十=(整数)(h*0.1);
//3.计算图像的高度
int图像高度=(int)(h*0.7);
//4.计算图像宽度,其比例为3:4
int图像宽度=(int)(图像高度/4)*3;
//5.计算每种宽度能容纳多少人(当然要有一些填充物
int=20;
int minimumImageArea=(int)图像宽度+(填充*2);
int columns=(int)Math.floor(metrics.widthPixels/minimumImageArea);
//6.偏移和乘数
int heightOffSet=(int)h-百分之十-图像高度;
int-widthOffSet=(int)((w/columns)/2)-(imageWidth/2);
int高度乘数=h;
整数宽度乘数=(整数)(带列);
int行=0;
int计数器=0;
对于(intx=0;x<4;x=x+1){
FetchableImageView image=新的FetchableImageView(此为空);
LinearLayout.LayoutParams params=新的LinearLayout.LayoutParams(图像宽度、图像高度);
image.setImageResource(R.drawable.issue);
image.setId(1);
参数设置边距(宽度偏移+(计数器*宽度乘数),高度偏移+(行*高度乘数),0,0);
linearLayout.addView(图像,参数);
image.setImage(“http://upload.wikimedia.org/wikipedia/en/2/22/Seventeen_Magazine_cover.jpg");
计数器++;
if(计数器==列)
{
计数器=0;
行++;
}
}
}
找到了解决方案

让父母成为亲戚

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

FetchableImageView image=新的FetchableImageView(此,空);
RelativeLayout.LayoutParams参数=新的RelativeLayout.LayoutParams(270,360); image.setImageResource(R.drawable.issue); 参数设置边距(20,20,0,0); relativeLayout.addView(图像,参数); image.setImage(“”); 图片.背景颜色(颜色.红色)

/************************************
 * 
 ************************************/
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++;
        }
    }


}