以编程方式将其添加到FrameLayout后无法看到View

时间:2016-07-11 23:52:08

标签: android android-videoview

我正在实施自己的VideoView版本。该课程包含一个播放暂停按钮ImageView,覆盖VideoView。一切都被FrameLayout包裹着。要使用此类,我调用一个自定义初始化方法,该方法从其父级中删除该类,以编程方式创建FrameLayoutImageView,然后将VideoViewImageView添加到FrameLayout。最后,FrameLayout作为原始索引添加到VideoView的父级。问题是该视图对用户不可见,但我可以看到父进程在调试时将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);
        }
    });
}

1 个答案:

答案 0 :(得分:2)

尝试playPauseImageView.bringToFront(); 并且您应该在将视图添加到viewGroup

时设置视图LayoutParams
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
videoViewLayout.addView(playPauseImageView, layoutParams);
playPauseImageView.bringToFront();