android中statusbar高度的問題

來源:互聯網
上載者:User

team老大讓我找找怎麼擷取statusbar的高度,但是找來找去找不到android SDK中有描述statusbar高度的資訊,其實在android的framework中的xml檔案中有描述,高度是25dp(在frameworks\base\core\res\res\values\dimens.xml),但是google並沒有在sdk中公開,不知道為什麼這麼做,假如手機廠商修改了這個高度,不知道我們應該怎麼從sdk中擷取這個參數。

其實大部分手機都不會修改這個參數值(我目前還不知道有手機不是這個值的),因為這樣會跟著修改很多東西,例如statusbar上的表徵圖大小、字型大小都要改變。其實是可以用程式擷取到這個數值的,只不過這個數值不是以dp為單位,而是以px為單位,但是前提是必須通過view才能擷取,有些不包含view的程式就不知道怎麼擷取了,希望有高手可以幫幫忙。

附上用view擷取statusbar高度的的代碼(代碼不能放在onCreate方法):

public class CalculateStatusBar extends Activity {    /** Called when the activity is first created. */private TextView tv;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        tv = (TextView) this.findViewById(R.id.myTextView);        tv.post(new Calculater());    }        class Calculater implements Runnable {    public void run(){    Rect rect = new Rect();    getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);    int statusBarHeight = rect.top;    System.out.println("StatusBarHeight:" + statusBarHeight);    }    }}

程式啟動並執行結果和使用的手機解析度有關,具體結果如下:

LDPI:19px  (25*0.75)

MDPI:25px  (25*1.00)

HDPI:38px  (25*1.50)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.