2011-12-22 3 views
0

私は自分のButtonビュー(android Buttonクラスから拡張されていません)を生成しています。すべてのボタンに対して同じ高さを動的に調整するにはどうすればいいですか?

ボタンの高さは、電話機の画面のサイズによって異なります。このアプリは、すべての携帯電話の画面のすべてのボタンに同じ高さを表示する必要があります。screenサイズがすべて可能です。

次に、layoutに追加する前に、ボタンの高さのサイズを知っておく必要がありますが、画面上にペイントされるまで、サイズはわかりません。

どのように私はすべての私のボタンに同じサイズを置くことができますdynamically画面のサイズに応じて?

ありがとう

+0

@Caspar KleijneでonLayoutとレイアウト呼び出しでそれをやった、uが解決策を試してみましたか? –

+0

私はOPではない、私はちょうど投稿を編集した。 –

答えて

0

これを試してください。 ディメンションの電話機を320 x480と言うと、その上にある単一のボタンの幅と高さを確認できます。以下に示すクラスは、デバイスの幅と高さに基づいて幅と高さを計算します。あなたのボタンの幅と高さを40にする必要があることを知りたければ、calculateWidth()/ calculateHeight()のパラメータとして40を渡します。 ランタイムでは幅と高さが動的に設定されます。

public class DimensionManager { 

    private int _screenWidth; 
    private int _screenHeight; 
    private int _baseScreenWidth = 320; 
    private int _baseScreenHeight = 480; 

    public DimensionManager(Activity act) { 
     WindowManager w = act.getWindowManager(); 
     Display d = w.getDefaultDisplay(); 
     _screenWidth = d.getWidth(); 
     _screenHeight = d.getHeight(); 
    } 
/** 
* 
* @param baseViewWidth Width considering a base Layout. 
* @return Returns corresponding width for the device and orientation. 
*/ 
    public int calculateWidth(int baseViewWidth) { 
     return (baseViewWidth * _screenWidth)/_baseScreenWidth; 
    } 
    /** 
    * 
    * @param baseViewHeight Height considering a base Layout. 
    * @return Returns corresponding height for the device and orientation. 
    */ 
    public int calculateHeight(int baseViewHeight) { 
     return (baseViewHeight * _screenHeight)/_baseScreenHeight; 
    } 
} 
関連する問題