【问题标题】:Set position by percent - Android DisplayMetrics按百分比设置位置 - Android DisplayMetrics
【发布时间】:2016-11-09 18:20:49
【问题描述】:

我喜欢在我的应用中对所有位置使用百分比。我总是使用相同的系统。我是android编程的新手。

这是课程:

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%。

也许我的错误是 int - float 之间的关系

这是屏幕截图(FrameLayout): 我把这个代码放在这个按钮上 - 这意味着 x = 0 , y = 0 ,宽度 = 屏幕的一半,但它不是:

 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 ?! 

【问题讨论】:

    标签: android set position percentage


    【解决方案1】:

    最好的答案是:不要使用百分比。它不会成功的。使用 RelativeLayout 和 layout_gravity center_vertical 等属性。

    【讨论】:

    • 我需要对此进行更多探索,“不要使用百分比”不是我的解决方案。
    • 我会接受这个 A,因为我在标准 UI 方法中没有找到按屏幕尺寸进行全直径响应的解决方案。如果在画布技术中工作,则某些边距或边框会出现问题。
    猜你喜欢
    • 2013-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2012-06-16
    • 2018-10-13
    • 1970-01-01
    相关资源
    最近更新 更多