安卓零碎知識集中(2)

來源:互聯網
上載者:User

標籤:android   安卓   技術   

1、擷取標題列的高度

Rect frame = new Rect();getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);int statusBarHeight = frame.top;int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();int titleBarHeight = contentTop - statusBarHeight;textView.setText("statusBarHeight" + statusBarHeight + ";contentTop="+ contentTop + ";titleBarHeight" + titleBarHeight);

2、以Acivity作為布局

註:該類必須繼承ActivityGroup

LocalActivityManager activityManager;activityManager = getLocalActivityManager();View view2 = activityManager.startActivity("act2",new Intent(this, Act2.class)).getDecorView();linearLayout.addView(view2, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

3、螢幕並儲存

View view = getWindow().getDecorView();Bitmap bitmap = Bitmap.createBitmap(view.getWidth(),view.getHeight(), Config.RGB_565);                  view.draw(new Canvas(bitmap));              imageView.setImageBitmap(bitmap);

4、android 擷取組件尺寸大小

在oncreate()中利用view.getWidth()或是view.getHeiht()來擷取view的寬和高,看似沒有問題,其實他們去得值是0,並不是你想要的結果?

這是為什麼呢?

在調用oncreate()方法時,介面處於不可見狀態,記憶體載入組件還沒有繪製出來,你是無法擷取他的尺寸。

那如何在繪製組件之前能擷取到該組件的尺寸大小呢?

這裡有三種方法,經過驗證的:

(1)

int width =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); int height =View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); view.measure(width,height); int height=view.getMeasuredHeight(); int width=view.getMeasuredWidth();

(2)
//增加組件繪製之前的監聽ViewTreeObserver vto =view.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {    @Override    public booleanonPreDraw() {        int height =view.getMeasuredHeight();        int width =view.getMeasuredWidth();        } }); 
(3)
//增加整體布局監聽ViewTreeObserver vto = view.getViewTreeObserver();  vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){     @Override     public voidonGlobalLayout() {       view.getViewTreeObserver().removeGlobalOnLayoutListener(this);          int height =view.getMeasuredHeight();      int width =view.getMeasuredWidth();      }  });
那麼,在activity進入運行期時,組件的尺寸擷取方法就很簡單了,直接getWidth()和getHeight().





         




安卓零碎知識集中(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.