我正在将此示例集成到我的Android应用中以包含WebView vimeo播放器:
https://github.com/droid28/VimeoVideo
当我尝试setContentView时遇到错误。当我setContentView时,它会禁用我在我的活动中的按钮(好的和删除)。我试图用addView替换setContentView,指定要添加的视图,但后来我得到错误“指定的孩子已经有父。你必须首先在孩子的父母上调用removeView()”
如何在按钮工作的同时使用setContentView?
媒体预览活动
public class MediaPreview extends Main implements YouTubePlayer.OnInitializedListener {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media_preview);
okay = (ImageButton) findViewById(R.id.okay_imagePreview);
delete = (ImageButton) findViewById(R.id.delete_imagePreview);
okay.setOnClickListener(new OnClickListener() {
....
});
delete.setOnClickListener(new OnClickListener() {
....
});
setUpPreview();
}
private void setUpPreview(){
if(mediaURL.toLowerCase().contains("vimeo")){
Log.d(TAG, "adding vimeo");
isVimeoVideo = true;
vimeoPlayer = new HTML5WebView(this);
vimeoPlayer.loadUrl(mediaURL);
setContentView(vimeoPlayer.getLayout());
}
}
HTML5WebView
public class HTML5WebView extends WebView {
private void init(Context context) {
mContext = context;
Activity a = (Activity) mContext;
mLayout = new FrameLayout(context);
mBrowserFrameLayout = (RelativeLayout) LayoutInflater.from(a).inflate(R.layout.activity_media_preview, null);
mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.media_holder);
mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
// Configure the webview
WebSettings s = getSettings();
s.setLoadWithOverviewMode(true);
s.setSaveFormData(true);
s.setJavaScriptEnabled(true);
mWebChromeClient = new MyWebChromeClient();
setWebChromeClient(mWebChromeClient);
setWebViewClient(new WebViewClient());
mContentView.addView(this);
}
}
activity_media_preview.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
tools:context=".MediaPreview" >
<FrameLayout
android:id="@+id/media_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom_button_image_preview"
android:layout_alignParentTop="true"
android:background="@color/black"
android:gravity="center" >
<FrameLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black"
></FrameLayout>
</FrameLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/bottom_button_image_preview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:background="@color/white" >
<ImageButton
android:id="@+id/delete_imagePreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_action_discard"
android:background="@drawable/custom_button_blue"
android:contentDescription="@string/delete_imagePreview"
android:layout_marginRight="1dp"
>
</ImageButton>
<ImageButton
android:id="@+id/okay_imagePreview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:adjustViewBounds="true"
android:maxHeight="60dp"
android:maxWidth="60dp"
android:scaleType="fitCenter"
android:src="@drawable/ic_action_accept_white"
android:background="@drawable/custom_button_blue"
android:contentDescription="@string/okay_imagePreview"
>
</ImageButton>
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:1)
使用setContentView时,您告诉活动显示提供的视图。因此它只能以这种方式显示1个视图。但是你可以在1个视图中分层多个视图,现在这就是你应该做的。因此,您应该像FrameLayout一样创建容器视图,然后将vimeo视图添加到该容器中。
在您的情况下,您必须执行以下操作:
setContentView(R.layout.activity_media_preview);
FrameLayout frame = (FrameLayout) this.findViewById(R.id.main_content);
HTML5WebView vimeoView = HTML5WebView.init(this);
frame.addView(vimeoView);
答案 1 :(得分:0)
这就是我最终修复它的方式。它有点hacky,因为它最终创建了两组图像按钮,但我隐藏了从HTML5WebView创建的按钮。
<强> MediaPreview.java 强>
public class MediaPreview extends Main implements YouTubePlayer.OnInitializedListener {
FrameLayout mainContentView;
protected void onCreate(Bundle savedInstanceState) {
mainContentView = (FrameLayout) findViewById(R.id.main_content);
setUpPreview();
...
}
private void setUpPreview(){
if(mediaURL.toLowerCase().contains("vimeo")){
Log.d(TAG, "adding vimeo");
isVimeoVideo = true;
vimeoPlayer = new HTML5WebView(this);
vimeoPlayer.loadUrl(mediaURL);
mainContentView.addView(vimeoPlayer.getLayout());
}
}
<强> HTML5WebView.java 强>
public class HTML5WebView extends WebView {
private void init(Context context) {
mContext = context;
Activity a = (Activity) mContext;
mLayout = new FrameLayout(context);
mBrowserFrameLayout = (RelativeLayout) LayoutInflater.from(a).inflate(R.layout.activity_media_preview, null);
mContentView = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.main_content);
mCustomViewContainer = (FrameLayout) mBrowserFrameLayout.findViewById(R.id.media_holder);
okayButton = (ImageButton) mBrowserFrameLayout.findViewById(R.id.okay_imagePreview);
deleteButton = (ImageButton) mBrowserFrameLayout.findViewById(R.id.delete_imagePreview);
okayButton.setVisibility(View.GONE);
deleteButton.setVisibility(View.GONE);
mLayout.addView(mBrowserFrameLayout, COVER_SCREEN_PARAMS);
// Configure the webview
WebSettings s = getSettings();
s.setLoadWithOverviewMode(true);
s.setSaveFormData(true);
s.setJavaScriptEnabled(true);
mWebChromeClient = new MyWebChromeClient();
setWebChromeClient(mWebChromeClient);
setWebViewClient(new WebViewClient());
mContentView.addView(this);
}
public FrameLayout getLayout() {
return mLayout;
}
}