You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android中如何让View尺寸与自定义TextureView保持一致?

解决TapView与自定义TextureView尺寸对齐且避免全屏覆盖的问题

首先,咱们来拆解下当前问题的核心原因:

  • 你的tapView用了已废弃的fill_parent(现在推荐用match_parent)作为宽高,这会让它直接撑满父FrameLayout的可用空间;而父FrameLayout的宽高是wrap_content,它会被最大的子View(也就是tapView)拉扯到全屏,自然就覆盖了下方的另一个FrameLayout。
  • 同时,tapView的尺寸没有和你代码动态设置的MyTextureView同步,导致两者尺寸完全不匹配。

下面是具体的解决步骤:

1. 调整XML布局,修正tapView的初始参数

把tapView的宽高从fill_parent改成wrap_content,这样它初始不会撑大父容器,且FrameLayout的子View默认会左上角对齐,只要后续尺寸同步,位置自然就和TextureView一致:

<FrameLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_alignParentTop="true" 
    android:background="#000000" 
    android:id="@+id/frame"> 
    <View 
        android:id="@+id/tapView" 
        android:background="#0000ff" 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"/> 
    <com.example.yApp.MyTextureView 
        android:id="@+id/texture" 
        android:background="#ff0000" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
</FrameLayout> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true"> 
    <!-- some code --> 
</FrameLayout>

2. 代码中同步TapView与TextureView的尺寸

因为你的MyTextureView是通过代码设置尺寸的,所以分两种场景处理:

场景一:设置固定尺寸时同步

如果TextureView的尺寸是固定值,在设置它的参数时直接同步给tapView:

MyTextureView textureView = findViewById(R.id.texture);
View tapView = findViewById(R.id.tapView);

// 假设你要给TextureView设置目标宽高
int desiredWidth = 640;
int desiredHeight = 480;

// 设置TextureView的布局参数
FrameLayout.LayoutParams textureParams = new FrameLayout.LayoutParams(desiredWidth, desiredHeight);
textureView.setLayoutParams(textureParams);

// 同步给TapView
tapView.setLayoutParams(new FrameLayout.LayoutParams(textureParams.width, textureParams.height));

场景二:监听TextureView尺寸动态变化

如果TextureView的尺寸会动态改变(比如视频播放时的比例切换),给它添加布局变化监听器,实时同步tapView的尺寸:

MyTextureView textureView = findViewById(R.id.texture);
View tapView = findViewById(R.id.tapView);

textureView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, 
                               int oldLeft, int oldTop, int oldRight, int oldBottom) {
        // 计算TextureView当前的宽高
        int currentWidth = right - left;
        int currentHeight = bottom - top;
        
        // 更新TapView的尺寸
        FrameLayout.LayoutParams tapParams = new FrameLayout.LayoutParams(currentWidth, currentHeight);
        tapView.setLayoutParams(tapParams);
    }
});

这样处理后,tapView会始终和MyTextureView尺寸一致、位置重合,父FrameLayout也只会包裹它们的大小,不会再覆盖下方的布局啦。

内容的提问来源于stack exchange,提问作者mrneumann888

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:55:10