gpt4 book ai didi

android - 以编程方式将 View 添加到 FrameLayout 后看不到 View

转载 作者:搜寻专家 更新时间:2023-11-01 09:45:21 25 4
gpt4 key购买 nike

<分区>

我正在实现我自己的 VideoView 版本。该类包含一个播放暂停按钮作为 ImageView,它覆盖了 VideoView。一切都由 FrameLayout 包装。为了使用这个类,我调用了一个自定义初始化方法,该方法从其父类中删除该类,以编程方式创建 FrameLayoutImageView,然后添加 VideoViewImageViewFrameLayout。最后,FrameLayout 作为原始索引添加到 VideoView 的父级。问题是 View 对用户不可见,尽管我可以在调试时看到父级将 FrameLayout 作为其子级。

public class PodoVideoView extends VideoView {

// boilerplate constructors removed

// This method must be called to set the functionality of this class.
// reasoning: cannot call getParent() from constructor
public void init(Activity activity) {

// remove VideoView and save its index
ViewGroup parent = (ViewGroup) this.getParent();
int indexInParent = parent.indexOfChild(this);
parent.removeView(this);

// setup layout
final FrameLayout videoViewLayout = new FrameLayout(activity);
videoViewLayout.setBackgroundColor(activity.getResources().getColor(R.color.black));
videoViewLayout.setLayoutParams((new FrameLayout.LayoutParams(
LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT)));

// add VideoView and play/pause ImageView to layout
videoViewLayout.addView(this);
final ImageView playPauseImageView = new ImageView(activity);
playPauseImageView.setImageResource(R.drawable.video_play);
videoViewLayout.addView(playPauseImageView);

// add layout to parent
parent.addView(videoViewLayout, indexInParent);

// play/pause UX
setOnPreparedListener(new OnPreparedListener() {
@Override
public void onPrepared(final MediaPlayer mp) {
setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(isPlaying()) {
playPauseImageView.setVisibility(View.VISIBLE);
mp.pause(); // if this doesn't work, use mp.pause() instead of pause()
} else {
playPauseImageView.setVisibility(View.INVISIBLE);
mp.start();
}
}
});
}
});

setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
playPauseImageView.setVisibility(View.VISIBLE);
}
});
}

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