Я новичок в android. Я начинаю пытаться построить библиотеку методов. Мне гораздо удобнее в java, и поскольку все в runtime, кажется, происходит в java, я пытаюсь создать методы, связанные с GUI в java. У меня пока не так много, и я ищу, чтобы определить, на какие 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
}
Поэтому, если у меня есть кнопка с именем button, и я говорю setSizeByRatio (кнопка, 25, 50); он устанавливает высоту кнопки до 25% процента экрана и ее высоту до 50% от любого экрана.
Мой главный вопрос: как вы устанавливаете положение x и y, которое хотите, чтобы он начинал рисовать, как в reg java? Я пробежал макет (l, t, r, b); но он устанавливает только х и у относительно родителя.
Следующие вопросы - это еще что касается методов GUI, которые я должен изучить? и я знаю, что это убивает многих людей, но как я могу комментировать в XML? Я новичок в XML, как и я.
Это не очень актуально, но он очищает одну строку кода (может использоваться как ссылка для очистки большего количества строк кода, если у вас есть больше таких вещей).
Таким образом, 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;
}
}