2012-03-15 17 views
1

私はアンドロイドを新しくしています。私はメソッドライブラリを構築しようとしています。私ははるかにJavaで快適で、実行時のすべてがJavaで発生するようだから、私はJavaでGUIを扱うメソッドを構築しようとしています。私はそれほど遠くないし、オブジェクトを描画するためにxとyの位置を定義するために離れて探している。これは私がこれまで持っているものです。Android:設定xとy pos

//method to get width or Xpos that is a one size fits all 
public int widthRatio(double ratioIn){ 
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    double screenWidth = dm.widthPixels;  //gets screen height 
    double ratio = screenWidth/100;   //gets the ratio in terms of % 
    int displayWidth = (int)(ratio*ratioIn); //multiplies ratio desired % of screen 
    return displayWidth; 
} 

//method to get height or Ypos that is a one size fits all 
public int heightByRatio(double ratioIn){ 
    DisplayMetrics dm = new DisplayMetrics(); //gets screen properties 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    double screenHeight = dm.heightPixels; //gets screen height 
    double ratio = screenHeight/100;   //gets the ratio in terms of % 
    int newHeight = (int)(ratio*ratioIn);  //multiplies ratio by desired % of screen 
    return newHeight; 
} 

//sets size of any view (button, text, ...) to a one size fits all screens 
public void setSizeByRatio(View object, int width, int height){ 
    ViewGroup.LayoutParams params = object.getLayoutParams(); // gets params of view 
    params.width = widthRatio(width);       // sets width by portion of screen 
    params.height = heightByRatio(height);     // sets height by portion of screen 
} 

だから私はボタンという名前のボタンを持っている、と私は setSizeByRatio(ボタン、25、50)を言う場合。ボタンの高さを画面の25%に設定し、高さを画面の50%に設定します。

私の主な質問は、あなたがreg javaのように描画を開始したいxとyの位置をどのように設定するのですか? 私はレイアウト(l、t、r、b)を横断しました。親に対して相対的にxとyだけを設定します。

次の質問は、GUIメソッドを学ばなければならない点ですか?これが多くの人を殺すだろうが、XMLでコメントするにはどうすればいいのだろうか?私はXMLに慣れていて、私はアンドロイドです。

+0

チェック答えは:http://stackoverflow.com/questions/3441475/android-change-absolute-position-of-a-view-programmatically これは役立つかもしれません。 – Reena

+0

よく考えてみましょう。私はすべてが親になければならないと思う。たとえその親がメインレイアウトであっても。私はまだ高さと幅を行うことなく、xとyの位置を設定する方法を知りたいです。 – JBreezy901

+0

@Reenaありがとうございました。私は見た目が良くなる必要があると思う。私は良いことを探して、それを見ませんでした。 – JBreezy901

答えて

1

これは実際には関係ありませんが、1行のコードをクリーンアップします(このようなことが多い場合は、より多くの行のコードをクリーンアップするための参照として使用できます)。

このように、DisplayMetricsは、別の軸を取得するたびに入力するのではなく、両方に適用されます。この質問のための

//method to get width or Xpos that is a one size fits all 
DisplayMetrics dm = new DisplayMetrics() { // This line now applies to both int and gets screen properties 
public int widthRatio(double ratioIn){ 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    double screenWidth = dm.widthPixels;  //gets screen height 
    double ratio = screenWidth/100;   //gets the ratio in terms of % 
    int displayWidth = (int)(ratio*ratioIn); //multiplies ratio desired % of screen 
    return displayWidth; 
} 

//method to get height or Ypos that is a one size fits all 
public int heightByRatio(double ratioIn){ 
    getWindowManager().getDefaultDisplay().getMetrics(dm); 
    double screenHeight = dm.heightPixels; //gets screen height 
    double ratio = screenHeight/100;   //gets the ratio in terms of % 
    int newHeight = (int)(ratio*ratioIn);  //multiplies ratio by desired % of screen 
    return newHeight; 
    } 
} 
+0

ありがとう、それは有用であり、私は客観的に私のコードを見て嬉しいです。私は笑を得るために必要なすべての助けが必要です。私はそのようにすることを考えましたが、先生は常にグローバル変数を宣言していました。私はそれがちょうど立ち往生だと思う。そして、私はメソッドが外の変数の外にたくさんあるので、私はちょうど新しいプロジェクトにそれらをコピーし、過去することができるようにすることができます。しかし、私はそれをたくさん呼びたいと思っています。それを投げ捨てて新しいものを作成して何度も新しい – JBreezy901

関連する問題