查看布局怪异

时间:2014-05-31 09:24:44

标签: android android-linearlayout android-view layoutparams

有人能告诉我为什么会这样吗?

enter image description here

我要做的是让他们以对齐为中心,无论有多少。它可以很好地工作,甚至可以只使用1个,但4似乎这样做。我在代码中添加它们,这里:

    LinearLayout guessHolders = (LinearLayout)findViewById(R.id.guessHolders);
    guessHolders.removeAllViews();
    currentLetterPosition = 0;

    final Bitmap emptyLetterHolder = BitmapFactory.decodeResource(getResources(), R.drawable.letter_holder);

    for(int i=0; i<Globals.mUser.getLevel().getSolution().length(); i++)
    {
        final EmptyLetter tmp = new EmptyLetter(this, i);
        tmp.setImageBitmap(emptyLetterHolder);
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(Globals.defaultLetterSizes[mBlockSize], 
                Globals.defaultLetterSizes[mBlockSize], 1);
        parms.weight = 1;
        parms.gravity = Gravity.CENTER_HORIZONTAL;
        tmp.setLayoutParams(parms);         
        tmp.setAdjustViewBounds(false);         
        tmp.setOnClickListener(new View.OnClickListener() {             
            @Override
            public void onClick(View v) {
                Log.i("PP2", tmp.getId() + " << the clicked empty letter holder id");
                currentLetterPosition = tmp.getId();
                tmp.setImageBitmap(emptyLetterHolder);
                setChosenLetter(tmp.getId(), '\u0000');
            }
        });

        currentEmpties.add(tmp);            
        guessHolders.addView(tmp);
    }

我已尝试设置框视图的gravityweightsumwidthheight以及父容器LinearLayout,但似乎没有改变它们。

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

修正了它。有以下内容:

        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(Globals.defaultLetterSizes[mBlockSize], 
                Globals.defaultLetterSizes[mBlockSize], 0);
        parms.gravity = Gravity.CENTER_HORIZONTAL;
        tmp.setLayoutParams(parms); 

因此,我将加权设置为零,并将其余部分留给操作系统。现在适用于所有人。非凡。

干杯