开发者

Set size of a ToggleButton dynamicly

first of all sorry for my bad english :) I'm new on programming for Android... I'm trying to setup a view on runtime. I've created my own View class ( derived from AbsoluteView ) successfully, but if i try to resize the content of the view, nothing happens :(

Here is the full code i have wrote :

package com.CaliskanDevelopment.LottoGenerator;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.AbsoluteLayout;
import android.widget.ToggleButton;

@SuppressWarnings("deprecation")
public class CSelectNumbersLayout extends AbsoluteLayout 
{
private ToggleButton    m_clButton;

public CSelectNumbersLayout( Context    clContext )
{
    super( clContext );
    CreateButtons();
}

public CSelectNumbersLayout( Context        clContext,
                             AttributeSet   clAttributeSet )
{
    super( clContext, clAttributeSet );
    CreateButtons();
}

public CSelectNumbersLayout( Context        clContext,
                             AttributeSet   clAttributeSet,
                             int            i32DefStyle )
{
    super( clContext, clAttributeSet, i32DefStyle );
    CreateButtons();
}

private void CreateButtons()
{
    LayoutParams    clLayoutParams = (LayoutParams) generateDefaultLayoutParams();
    int             i32ButtonWidth = 10;
    int             i32ButtonHeight = 10;
    int             i32PosX = 0;
    int             i32PosY = 0;

    clLayoutParams.x = i32PosX;
    clLayoutParams.y = i32PosY;
    clLayoutParams.width = i32ButtonWidth;
    clLayoutParams.height = i32ButtonHeight;

    m_clButton = new ToggleButton( getContext() );
    m_clButton.setText( "setText" );
    m_clButton.setTextOn( "setTextOn" );
    m_clButton.setTextOff( "setTextOff" );
    m_clButton.setLayoutParams( clLayoutParams );

    this.addView( m_clButton );
}

protected void onSizeChanged( int   i32Width,
                              int   i32Height,
                              int   i32OldWidth,
                              int   i32OldHeight )
{
    int i;
    int i32ButtonWidth = ( i32Width / 7 );
    int i32ButtonHeight = ( i32Height / 7 );
    int i32PosX;
    int i32PosY;

    if ( i32ButtonWidth > i32ButtonHeight )
        i32ButtonWidth = i32ButtonHeight;
    else if ( i32ButtonHeight > i32ButtonWidth )
        i32ButtonHeight = i32ButtonWidth;

    i32PosX = ( i32Width / 2 ) - ( ( i32ButtonWidth * 7 ) / 2 );
    i32PosY = ( i32Height / 2 ) - ( ( i32ButtonHeight * 7 ) / 2 );

    LayoutParams    clLayoutParams = (LayoutParams) m_clButton.getLayoutParams();

    clLayoutParams.x = i32PosX;
    clLayoutParams.y = i32PosY;
    clLayoutParams.width = i32ButtonWidth;
    clLayoutParams.height = i32ButtonHeight;

    m_clButton.setLayoutParams( clLayoutParams );
    m_clButton.invalidate();

    this.invalidate();
}
}

I'm trying to resize the created buttons in function onSizeChanged开发者_运维技巧, because i have readed that this is the first time where i know how big is my view. Calling things like

m_clButton.invalidate();
m_clButton.postInvalidate();
this.invalidate();

does not have the desired result. The View is inside a Tab. If i click on the tab first time to show this Layout the Buttons are 10 x 10 ( as i created in the constructor ), i have debugged the Code, it runs over onSizeChanged but the buttons are not resized. If i show another view and click at this tab again the buttons are resized.

I think the problem is to refresh the view in function onSizeChanged(), but how to do this ???

Thanks in advance...

Hasan Caliskan

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜