按百分比设置排名-Android DisplayMetrics [英] Set position by percent - Android DisplayMetrics
问题描述
我喜欢对应用中的所有职位使用百分比.我总是使用相同的系统.我是android编程的新手.
I like to use percents for all position in my apps . I always use same system. I am new at android programming.
这是课程:
public class SCREEN {
DisplayMetrics dm = new DisplayMetrics();
Point size_ = new Point();
int width;
int height;
// DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
SCREEN (Context CONTEXT_) {
dm = CONTEXT_.getResources().getDisplayMetrics();
int densityDpi = dm.densityDpi;
height = dm.heightPixels;
width = dm.widthPixels;
}
// get full width
public int WIDTH() {
return width;
}
public int HEIGHT(){
return height;
}
public int W( int PER_ ){
return width/100*PER_;
}
public int H( int PER_ ){
return height/100*PER_;
}
}
用法示例:
EKRAN = new SCREEN();
MENU1.setX( (float) EKRAN.W( 50 ) );
这意味着MENU1按钮x的位置必须在屏幕中央,但不是.它的较小值较小,例如42%.
This means that MENU1 button x position must be on center of the screen but it isnt . Its little smaller value like 42%.
也许我的错误是int-float之间的关系
Maybe my bug is relationship between int - float
这是屏幕截图(FrameLayout): 我为该按钮放置了这段代码-这意味着x = 0,y = 0,width =屏幕的一半,但不是:
Here is screenshot (FrameLayout) : I put this code for this button - It means x = 0 , y = 0 , width = half of screen but it is not :
final Button BTN1 = new Button(this);
BTN1.setText( String.valueOf( EKRAN.H( 0 ) ) );
BTN1.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));
VEIW_MAIN.addView(BTN1);
BTN1.setY( (float) EKRAN.H( 0 ) );
BTN1.setX( (float) EKRAN.W( 0 ) );
BTN1.setWidth( (int) EKRAN.W( 50 ) );
Important : This work EKRAN.WIDTH()/5 = 216 , but EKRAN.W(20) = 200
My current screen i is 1080 . Only this EKRAN.WIDTH()/2 give me 540 .
Even EKRAN.WIDTH()/100*50 give me 500 ... Where is the 40pix ?!
推荐答案
最好的答案是:不要使用百分比.它不会解决.使用RelativeLayout和诸如layout_gravity center_vertical之类的属性.
The best answer is: Don't use percentages. It wont work out. Use RelativeLayout and properties like layout_gravity center_vertical and such things.
这篇关于按百分比设置排名-Android DisplayMetrics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!