Androidアプリエンジニアのゆーちゃまさんです^^
Webアプリ3年ほど、Androidアプリ開発10年ほどやっています。
- Viewの大きさが0でちゃんと取れない。。取得のしかたが分からない
この記事を読むことで、Viewの大きさを取得する方法がわかります!
実際に開発していて、よく利用するので知っておくと役にたちます^^
オンラインレッスン始めました!
zoomでのオンラインレッスンをはじめました^^
マンツーマンで対応しますので、自由に質問していただけます。
javaの基礎から、転職に関する相談、Androidアプリ開発・そのた幅広く対応します。
> Androidアプリの開発・プログラミング基礎を丁寧に教えます!
- マンツーマンでjava基礎、わからない部分を丁寧に解説!
- Androidアプリ開発の方法・技術的な解説
- ITエンジニア転職の相談
- ITスクール選びで悩んでいる
- 実際に現場で習得しておきたいことを教えます^^
など内容は幅広くたいおういたします^^
お気軽に相談してください!
結論: ViewTreeObserver を使って取得する
大きさをレイアウトファイルで固定に設定していないViewの大きさを取得するには…
ViewTreeObserverを使えば、Viewの幅・高さを取得できます!

実際に、コードを見ながら解説します!
Viewが作成されるまで大きさを取得できない
AndroidはViewが作成されてからでないと、幅や高さなどの大きさを取得することができません。

まずは普通にViewの幅・高さを取得してみます

画面中央に背景がピンクのTextViewがあるだけの単純な画面です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="gone"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:visibility="gone"/> <TextView android:id="@+id/testText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="あいうえおああ" android:layout_centerInParent="true" android:background="@color/ozpink_accent" /> </RelativeLayout> |

TextViewが真ん中にあるレイアウトxmlです^^
コードonCreate
1 2 3 4 5 6 7 8 | @Override protectedvoidonCreate(@NullableBundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_local_webview); // TextViewを取得 finalTextViewtextView=findViewById(R.id.testText); LogUtil.d(TAG,"textView.幅:"+textView.getWidth()+", 高さ:"+textView.getHeight()); |

単純にonCreate()でtextViewを取得して、幅・高さをログに出力しています
実行結果ログ確認


onCreate()実行時では、まだViewが作成されていないため幅・高さともに0になっています
viewTreeObserverを使用して幅・高さを取得してみる
では、実際にviewTreeObserverを使用してみましょう!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // TextViewを取得 finalTextViewtextView=findViewById(R.id.testText); LogUtil.d(TAG,"textView.幅:"+textView.getWidth()+", 高さ:"+textView.getHeight()); // viewTreeObserverで大きさを取得 textView.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener(){ @Override publicvoidonGlobalLayout(){ // Viewの大きさが決定すると、このonGlobalLayout()がコールされる LogUtil.d(TAG,"viewTreeObserver:textView.幅:"+textView.getWidth()+", 高さ:"+textView.getHeight()); // removeしないと何回もコールされてしまうので忘れずにremoveしましょう textView.getViewTreeObserver().removeOnGlobalLayoutListener(this); } }); |
取得したいViewに、onGlobalLayoutListenerを登録します。
そうすると対象のViewの大きさが決定した時点で、onGlobalLyout()のコールバックが呼ばれます。
12行目のremoveをしないと何回も呼ばれてしまうので、注意してください。
viewTreeObserverの実行結果・ログ確認


おお!
幅:294、高さ:57、ちゃんと取得できてますね^^
まとめ
実際の開発でもかなりの頻度で利用するViewTreeObserver。
あそこのViewと同じ幅にしたいんだけど…
という動的に大きさ設定する場面などで役立ちます。
簡単に使えますし^^
僕も初心者の頃は知らなくて、幅や高さを取得できなくて困った事があります。
ぜひ、活用してください!

↑押してもらえると励みになります^^

人気ブログランキング
こちらもよろしければご覧ください^^
⇒ ⇒ ⇒ 【java map】Mapの使い方、使いどころを実例で解説
⇒ ⇒ ⇒ 【プログラム勉強中】NullPointerException ぬるぽをちゃんと理解しよう!
コメント