I'm using a screen that I want to switch between layouts on the click of a button. I want both layouts to occupy the full height of the screen, when the button on the first layout is pressed, i want this layout to disappear and the other layout to take it's place, and then same on the next layout.

<LinearLayout 
    android:id="@+id/layoutFirst"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    ..... views

</LinearLayout>

<LinearLayout 
    android:id="@+id/layoutSecond"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    ..... views

</LinearLayout>

On each layout I have a button. I want this button to switch so the other layout occupies the full screen, but nothing happens. The code in the on-click event for the first screen is the following

LinearLayout layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutFirst.setVisibility(View.GONE);
LinearLayout layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);
layoutSecond.setVisibility(View.VISIBLE);

Just in case the problem was to do with the fill_parent, I also tried this with the height being set to wrap_content. Initially I can see both layouts on the screen, when I press the button, the layout still does nothing.

Can anyone tell me if I am doing something wrong or if there's a way to solve this issue. Any help would be greatly appreciated it

有帮助吗?

解决方案

Put the two LinearLayouts inside a FrameLayout:

<FrameLayout
    android:id="@+id/framelayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/layoutFirst"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:visibility="gone">
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutSecond"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    </LinearLayout>
</FrameLayout>

Give the LinearLayouts different ids!

其他提示

Is your parent layout a "RelativeLayout" ? => not necessary here.

Try also the solution proposed in this quite similar thread how to hide linearlayout from java code?

Update:

/*global or add final keyword if not global in order to use inside listener */
LinearLayout layoutFirst;
LinearLayout layoutSecond;
/*end global*/

/*in onCreate(Bundle) method*/
layoutFirst = (LinearLayout) this.findViewById(R.id.layoutFirst);
layoutSecond = (LinearLayout) this.findViewById(R.id.layoutSecond);

/*and then in your listener, alternatively*/
layoutFirst.setVisibility(View.GONE);
layoutSecond.setVisibility(View.VISIBLE);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top