在android中隐藏/显示视图的问题

时间:2016-01-23 10:43:29

标签: android android-layout android-view

我的应用程序包含4个视图的屏幕,以2 x 2的表格布局排列。每个视图都显示视频

enter image description here

每个视图都包含播放控件。当我按下按钮使 view3 全屏时,应用程序显示如下。 View2 重叠到全屏 view3 。我只希望将 View3 显示为全屏,并避免 View2

enter image description here

以下代码用于隐藏/显示视图

@Override
public void toggleFullScreen() 
{
    mbFullscreen = !mbFullscreen;
    mStrTmp = "";
    Trace((ViewGroup)getRootView(), mbFullscreen);
    mMessage.setText(mStrTmp);
}

private void Trace(ViewGroup layout, boolean bFullScreen ) {
    View FullScreenChild = null;
    ViewGroup FullScreenLayout = null;
    for( int i = 0; i < layout.getChildCount(); i++){
        View child = layout.getChildAt(i);
        if( child instanceof MtxVideoView ){
            if( child == this ){
                FullScreenChild = child;
                FullScreenLayout = layout;
            }
            layout.setVisibility(bFullScreen?View.GONE:View.VISIBLE);
            child.setVisibility(bFullScreen?View.GONE:View.VISIBLE);
        }
        else if (child instanceof ViewGroup) {
            Trace((ViewGroup) child, bFullScreen);
        }
    }

    if(bFullScreen){
        if( FullScreenLayout != null )
            FullScreenLayout.setVisibility(View.VISIBLE);

        if( FullScreenChild != null ){
            FullScreenChild.setVisibility(View.VISIBLE);
            mStrTmp = mStrTmp + "FullScreen";
        }
    }
}

预期输出如下所示

enter image description here

1 个答案:

答案 0 :(得分:1)

我有一个非常简单的解决方案。使用FrameLayout显示您的选择布局。在您的选择布局上添加ImageView,但保持不可见,如下所示: -

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

    <LinearLayout
        android:id="@+id/select_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <LinearLayout
                android:id="@+id/item_1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="#000"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Item 1"
                    android:textSize="25sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/item_2"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="#d5d5d5"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Item 2"
                    android:textSize="25sp" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/item_3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.5" >

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="#d5d5d5"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Item 3"
                    android:textSize="25sp" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/item_4"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:background="#000"
                android:gravity="center"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Item 4"
                    android:textSize="25sp" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <ImageView
        android:id="@+id/hidden_imageView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop" />

</FrameLayout>

结果将如下所示 enter image description here

现在单击此网格项会使隐藏的Imageview可见并相应地更改imageView的图像

findViewById(R.id.item_1).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            findViewById(R.id.select_image).setVisibility(View.GONE);
            findViewById(R.id.hidden_imageView).setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.hidden_imageView))
                    .setBackgroundResource(R.drawable.walking);

        }
    });

    findViewById(R.id.item_2).setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            findViewById(R.id.select_image).setVisibility(View.GONE);
            findViewById(R.id.hidden_imageView).setVisibility(View.VISIBLE);
            ((ImageView) findViewById(R.id.hidden_imageView))
                    .setBackgroundResource(R.drawable.ic_launcher);

        }
    });

......等等

并导航回使用点按图像视图

findViewById(R.id.hidden_imageView).setOnClickListener(
            new OnClickListener() {

                @Override
                public void onClick(View v) {

                    findViewById(R.id.select_image).setVisibility(
                            View.VISIBLE);
                    findViewById(R.id.hidden_imageView).setVisibility(
                            View.GONE);

                }
            });

enter image description here

即使你旋转屏幕也很好看

enter image description here

希望它会对你有所帮助。