gpt4 book ai didi

android - 在 android 中隐藏/显示 View 的问题

转载 作者:行者123 更新时间:2023-11-29 01:24:18 25 4
gpt4 key购买 nike

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

enter image description here

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

enter image description here

以下代码用于隐藏/显示 View

@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

最佳答案

我为您准备了很多简单的解决方案。使用 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);

}
});

……等等

然后像这样在 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);

}
});

enter image description here

即使旋转屏幕也很好看

enter image description here

希望对您有所帮助。

关于android - 在 android 中隐藏/显示 View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34962450/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com