Android: using FEATURE_NO_TITLE with a custom ViewGroup leaves space on top of the window

I am trying to create a custom ViewGroup and I want to use it with a fullscreen application. I use "requestWindowFeature (Window.FEATURE_NO_TITLE)" to hide the title bar. The title bar is not displayed, but still takes up space on top of the window.

Here is a picture of the problem

The image above was created using the following code:

public class CustomLayoutTestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); Button b = new Button(this); b.setText("Hello"); CustomLayout layout = new CustomLayout(this); layout.addView(b); setContentView(layout); } } public class CustomLayout extends ViewGroup { public CustomLayout(Context context) { super(context); } public CustomLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { Log.i("CustomLayout", "changed="+changed+" l="+l+" t="+t+" r="+r+" b="+b); final int childCount = getChildCount(); for (int i = 0; i < childCount; ++i) { final View v = getChildAt(i); v.layout(l, t, r, b); } } } 

(The full Eclipse project is here )

It is interesting to see what exactly Android is provided for this custom layout. I configure CustomLayout as the root layout of my activity. OnLayout magazine gets "t = 25", and this is what repels my layout. What I don't know is what I'm doing wrong, which does Android "t = 25" (this is exactly the height of the title bar).

I run this code in Android SDK 2.1, but I also meet in Android 2.2.


EDIT . If I change the CustomLayout class for some default layout (e.g. LinearLayout), the space will disappear. Of course, the default layouts of the Android SDK do not create the layout that I am trying to create, so I create it.

Although the layout I am creating is somewhat complex, it is the smallest code I could create in order to reproduce the problem that I have with my layout.

+6
source share
2 answers

This is not a complete answer, but for now, you can work around the problem by wrapping your own layout in <FrameLayout />

In addition, it is worth noting that your layout goes beyond the screen. It has shifted down the header height (38 pixels in my emulator)

Edit: Got it. onLayout () (and the corresponding layout () method) indicate that the coordinate does not apply to the top of the screen, they refer to the parent ( http://developer.android.com/reference/android/view/View.html#layout%28int, % 20int,% 20int,% 20int% 29 ). Thus, the system tells you that you are in relative coordinates (0, 38), and you add them when you pass this to your child, which means that you say that your child is in screen coordinates (0, 76) causing a break.

Actually you want:

 v.layout(0, 0, r - l, b - t); 

This will put your child view in line with the top left corner of your view with the same width and height as your view.

+3
source

I had the same issue with FrameLayout in 2.2

I fixed it by adding android:layout_gravity="top" in FrameLayout

0
source

Source: https://habr.com/ru/post/903154/


All Articles