6

Currently I have several views and the screen is divided into two section

Example:

text1     image1

text2

text3

text4

The problem is , if the image1 is tall , it will overlap the textview on left side, so I use to left of to force the textview width not exceed the imageview's left.

 android:layout_toLeftOf="@id/imageView1" 

however, every textview are aligned to left of the imageview as I do not know the height of it until the view is created . And I want all textview below the baseline of imageview remove the layout rules of android:layout_toLeftOf

So I search for the solution and find two ways ?

1.onWindowFocusChanged

2.getViewTreeObserver().addOnGlobalLayoutListener

Both can get the yaxis of the view.

The problems are:

 1. what is the difference between them ?

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

Code:

        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);
            }
        }
    });

The error is I would like to set a global value to store the y of imageview, but it warns The final local variable ImgY cannot be assigned, since it is defined in an enclosing type Also, the removeRule function return

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

Thanks a lot for helping.

user782104
  • 13,233
  • 55
  • 172
  • 312

0 Answers0