2013-08-02 23 views
5

は現在、私はいくつかのビューを持っており、画面には2つのセクションに分割されaddOnGlobalLayoutListenerとonWindowFocusChangedの違いは?

例:画像1は、背が高い場合、問題は、それが左側のTextViewが重複するので、私が使用

text1  image1 

text2 

text3 

text4 

テキストビューの幅をイメージビューの左端を超えないように強制します。

android:layout_toLeftOf="@id/imageView1" 

ただし、すべてのテキストビューは、ビューが作成されるまでわからないため、イメージビューの左側に揃えられています。そして、イメージビューのベースラインの下のすべてのテキストビューがレイアウトルールを削除するようにしますandroid:layout_toLeftOf

私は解決策を探して2つの方法を見つけますか?

1.onWindowFocusChanged 

2.getViewTreeObserver().addOnGlobalLayoutListener 

どちらもビューのy軸を取得できます。

問題は、次のとおりです。

1. what is the difference between them ? 

2. I tried approach 2 , but it is not working, how to fix it? 

コード:

 image.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      ImgY = image.getY(); 
     } 
    }); 

    lng.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 
     @Override 
     public void onGlobalLayout() { 
      if (lng.getY() > ImgY) { 
       lng.removeRule(RelativeLayout.LEFT_OF); 
      } 
     } 
    }); 

エラーが、私はImageViewののYを格納するためのグローバルな値を設定したいですが、それはまたThe final local variable ImgY cannot be assigned, since it is defined in an enclosing typeを警告し、removeRule関数戻り

The method removeRule(int) is undefined for the type TextView 

ありがとうございました。

答えて

0

このアプローチではなく、TextViewのテキストでインライン画像を使用できます。 thisをご覧ください。

関連する問題