Question

I am trying to program an alternative landscape view file (an xml file) for my app, and I must use FrameLayout instead of LinearLayout (that's what the book said). But Framelayout does not stack well, so we are supposed to use android:layout_gravity and then assign an x/y dimension, for example: android:layout_gravity="center". This example centers something exactly in the middle (both vertically and horizontally).

But my problem is, I have 4 levels on the vertical plane, where I want to place things. A text line, a line with 2 buttons (true, false), another button (cheat), then finally a line with 2 arrow buttons (previous and next). But with the layout_gravity, they only have very crude placements: top, center, bottom. I noticed that if you do not assign anything, they all end up in the upper left corner.

So how do I stack these vertically so they fall nicely spaced from top to bottom? Assigning 2 things the same parameters does not stack them, but rather overlaps them terribly.

Thank you for your help, below is my code. I have not put any gravity layouts in there yet.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView 
        android:id="@+id/question_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp" />        

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/true_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/true_button" />

        <Button
            android:id="@+id/false_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/false_button" />

    </LinearLayout>

    <Button 
        android:id="@+id/cheat_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cheat_button" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageButton
        android:id="@+id/prev_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_left"
        android:contentDescription="@string/move_back"
         />

        <ImageButton
        android:id="@+id/next_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"
        android:contentDescription="@string/move_forward"
        />

    </LinearLayout>

</FrameLayout>  
Was it helpful?

Solution

You should use a Relative Layout or Linear Layout to achieve this because the Frame Layout is simply not designed for this. Here is the api documentation for frame layout-

FrameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.

So controlling the position of child in Frame layout is very much limited,ie only using gravity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top