【问题标题】:Issues with hiding/showing views in android在 android 中隐藏/显示视图的问题
【发布时间】:2016-01-24 19:44:06
【问题描述】:

我的应用由屏幕组成,4 个视图以 2 x 2 的表格布局排列。每个视图都显示视频

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

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

@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";
        }
    }
}

预期输出如下所示

【问题讨论】:

    标签: android android-layout android-view


    【解决方案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>
    

    结果将如下所示

    现在点击这个网格项目使隐藏的 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);
    
            }
        });
    

    ...等等

    然后像这样点击 imageView 并返回导航

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

    即使旋转屏幕也很好看

    希望对您有所帮助。

    【讨论】:

    • 您的解决方案很好。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多