0

This what i get in my Activity:

http://img855.imageshack.us/img855/2056/androidscreenshot.jpg

The activity is filled by a TableLayout. The TableLayout in turn is filled by 2 TableRow that are Two View containing a 100x100 cyan bitmap. The yellow area is the background of the activity in position (1,1) of the TableView. The red area is the background of the activity in position (1,2).

The problem is that i cannot see the other two cyan bitmap in position (2,1) and (2,2). I think the problem is that the View in the first row are resized in the width but not in the height. So the view in the first row obscure the view in the second row.

If i add to the View both ViewGroup.LayoutParams or LayoutParams with WRAP_CONTENT i get a java.lang.ArithmeticException: divide by zero

This is my code:

public class HIDActivity extends Activity
{

    boolean toggle = false;            
    TableLayout IFaceLayout;
    TableRow TButtons1;
    TableRow TButtons2;
    TableRow TArea;
    SimpleButton TButton1;
    SimpleButton TButton2;
    SimpleButton TButton3;
    SimpleButton TButton4;


    public void onCreate(Bundle icicle)
    {
        super.onCreate(icicle);


        TButton1 = new SimpleButton(this);
        TButton2 = new SimpleButton(this);
        TButton3 = new SimpleButton(this);
        TButton4 = new SimpleButton(this);

        TButtons1 = new TableRow(this);
        TButtons2= new TableRow(this);

        TButtons1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
        TButtons2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );

        TButtons1.addView(TButton1);                
        TButtons1.addView(TButton2);
        TButtons2.addView(TButton3);
        TButtons2.addView(TButton4);

        IFaceLayout = new TableLayout(this);
        IFaceLayout.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );
        IFaceLayout.setStretchAllColumns(true);

        IFaceLayout.addView(TButtons1);
        IFaceLayout.addView(TButtons2);

        setContentView(IFaceLayout);
        IFaceLayout.requestLayout();
    }


    class SimpleButton extends View
    {
        BitmapDrawable ButtonImage;
        Paint Style;
        int i , j ;
        int width , heigth;

        public SimpleButton(Context context) 
        {
            super(context);
            Style = new Paint();

            if(toggle == false) 
            {    
                this.setBackgroundColor(Color.YELLOW);
                toggle = true;
            }    
            else 
            {    
                this.setBackgroundColor(Color.RED);
                toggle = false;
            }
        }

        @Override
        public void onDraw(Canvas canvas)
        {       
            ButtonImage = new BitmapDrawable(Bitmap.createBitmap(100 , 100 , Bitmap.Config.RGB_565)); 
            this.width = ButtonImage.getBitmap().getWidth();
            this.heigth = ButtonImage.getBitmap().getHeight();

            for(i = 0 ; i < width ; i++)
            {                
                for(j = 0 ; j < heigth ; j++)
                {
                    ButtonImage.getBitmap().setPixel(i, j, Color.CYAN);
                }
            }

            canvas.drawBitmap(ButtonImage.getBitmap() , 0 , 0 , Style);        
        }        
    }
}

Could someone please help me with this problem.

4

2 回答 2

0

Gophermofur的暗示我部分正确。

调用 MyView.setLayoutParams 并将与包含 MyView 的对象的类型匹配的 Layout 对象传递给它是正确的。然而,为了允许容器控制包含对象的大小,我必须使用 MATCH_PARENT 而不是 WRAP_CONTENT。

此外,我已经部分解决了手动将高度设置为 100 的问题。即:

TButton1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT , 100); 

现在我得到了 4 个正方形:

http://img152.imageshack.us/img152/1530/androidscreenshot3.jpg

于 2012-05-30T09:48:36.873 回答
0

问题似乎是扩展 View 的 SimpleButton 未设置为 wrap_content 垂直。

设置布局参数时,您希望使用对象所在的容器。当您定义布局参数时,您实际上是在说“这就是我的对象/视图将如何填充它所在容器的空间”。在这种情况下,您的按钮将位于表格行内,因此您应该定义相对于表格行的布局参数,如下所示:

TButton1.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );

TButton2.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );

TButton3. [same thing]
TButton4. [same thing]

您可能还需要将 TButtons 布局参数更改为相对于 TableLayout,因为 TableRow 位于表格布局中。

TButtons1.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );            
TButtons2.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) ); 

您的表格布局也包含在父布局中。我不确定它是什么,但它将是您的 xml 文件的根(可能),这很可能是相对或线性布局。您应该根据它所在的父布局定义表格布局参数。

IFaceLayout.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT , LayoutParams.WRAP_CONTENT ) );    
于 2012-05-29T15:08:46.277 回答