簡體   English   中英

如何在Android中獲取ViewPager的寬度和高度?

[英]How to get Width and Height of ViewPager in android?

我的活動中只有一個ViewPager。 我的活動的xml代碼是-

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
         android:id="@+id/pager"     
         android:layout_width="match_parent"
         android:layout_height="match_parent" />

我想知道它的高度和寬度(在這種情況下應該是我的應用程序覆蓋的整個區域)。 問題是,我想知道這些尺寸之前,我將數據發送到viewpager。

我嘗試使用以下內容-

pager= (ViewPager) findViewById(R.id.pager);
int width = pager.getWidth();
int height = pager.getHeight();

但它們總是返回0。

為什么回到零,在我的屬性設置為“match_parent”?

以及如何獲得尺寸?

編輯1 - 目前活動XML有viewpager孤獨,我應該把它放在線性/相對布局找到尺寸是多少?

謝謝。 問候,

即使你使用的getWidth()和ViewPager的的getHeight(),你不能得到任何尺寸。 因此,我嘗試通過使用子級高度以及您使用的任何邊距和填充來獲取ViewPager的高度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM