我正在尝试在 WebView 内全屏播放我的新闻应用程序中的视频。 Web View 是 Activity 的一部分,其中还包含其他布局和 View 。我可以使用 Web Chrome 客户端全屏播放视频,但问题是,当我单击全屏按钮时,视频确实会在全屏,但不是黑条,而是显示背景布局的内容,这是不可点击的,但看起来仍然不太好。我在互联网上绞尽脑汁,但没有找到有帮助的正确答案。 这是我的 xml 布局的一部分
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="@dimen/dim5dp"
android:layout_marginRight="@dimen/dim5dp"
android:fitsSystemWindows="true">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:scrollbars="none"
tools:ignore="WebViewLayout" />
</ScrollView>
这是我的 java 文件的一部分...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news_detail);
sharedObjects = new SharedObjects(NewsDetailActivity.this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.parseColor(sharedObjects.getHeaderColor()));
}
nestedScrollView=findViewById(R.id.scrollview);
initView();
rvCustom = (RecyclerView) findViewById(R.id.related);
rvCustom.setLayoutManager(new LinearLayoutManager(this));
ProgressDialogSetup();
NewsID = getIntent().getStringExtra(AppConstants.NEWS_ID);
Intent intent = getIntent();
if (intent != null) {
newsId = intent.getStringExtra("NewsID");
parentId = intent.getStringExtra("CATEGORY_IDS");
}
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NewsDetailActivity.this, AddCommentActivity.class);
intent.putExtra("NewsID", newsId);
intent.putExtra("TITLE", titlenews);
startActivity(intent);
}
});
btnview.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(NewsDetailActivity.this, CommentActivity.class);
intent.putExtra("NewsID", newsId);
intent.putExtra("TITLE", titlenews);
startActivity(intent);
}
});
setColorTheme();
}
private void ProgressDialogSetup() {
progressDialog = new ProgressDialog(NewsDetailActivity.this);
progressDialog.setMessage(getResources().getString(R.string.please_wait));
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setCancelable(false);
}
private void initView() {
toolbar = (android.support.v7.widget.Toolbar)findViewById(R.id.toolbar);
tvToolbarTitle = findViewById(R.id.tvToolbarTitle);
mIvBackBtn = (ImageView) findViewById(R.id.ivBackBtn);
ivDate = (ImageView) findViewById(R.id.ivDate);
ivComment = (ImageView) findViewById(R.id.ivComment);
ivShare = (ImageView) findViewById(R.id.ivShare);
mIvBackBtn.setOnClickListener(this);
mAppBar = (AppBarLayout) findViewById(R.id.AppBar);
mImgMain = (ImageView) findViewById(R.id.imgMain);
logo = (TextView) findViewById(R.id.tvToolbarTitle);
mTvCat = (TextView) findViewById(R.id.tvCat);
mTvTitle = (TextView) findViewById(R.id.tvTitle);
mTvPubDate = (TextView) findViewById(R.id.tvPubDate);
llMain = findViewById(R.id.llMain);
mTxtcomment = (TextView) findViewById(R.id.txtcomment);
mTxtcomment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewsDetailActivity.this, CommentActivity.class);
intent.putExtra("NewsID", newsId);
intent.putExtra("TITLE", titlenews);
startActivity(intent);
}
});
mTxtshare = (TextView) findViewById(R.id.txtshare);
tvrelated = (TextView) findViewById(R.id.related1);
btnadd = findViewById(R.id.btn_add);
btnview = findViewById(R.id.btn_view);
mWebView = (WebView) findViewById(R.id.webView);
logo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(NewsDetailActivity.this, HomePageActivity.class);
startActivity(i);
}
});
playbtn = findViewById(R.id.playbtn);
playbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(NewsDetailActivity.this, YoutubeActivity.class);
intent.putExtra("video_url", video_url);
startActivity(intent);
}
});
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setBackgroundColor(Color.TRANSPARENT);
mWebView.getSettings().setDefaultFontSize(16);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
WebSettings webSettings = mWebView.getSettings();
mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.TEXT_AUTOSIZING);
webSettings.setDomStorageEnabled(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setDatabaseEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
mWebView.setWebViewClient(new Browser_home());
mWebView.setWebChromeClient(new MyChrome());
mWebView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
mTxtshare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBodyText = link;
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject here");
sharingIntent.putExtra(Intent.EXTRA_TEXT, shareBodyText);
startActivity(Intent.createChooser(sharingIntent, "Shearing Option"));
}
});
imgFacbook = (CircleImageView) findViewById(R.id.imgFacbook);
imgFacbook.setOnClickListener(this);
imgTwitter = (CircleImageView) findViewById(R.id.imgTwitter);
imgTwitter.setOnClickListener(this);
imgPin = (CircleImageView) findViewById(R.id.imgPin);
imgPin.setOnClickListener(this);
imgLinkded = (CircleImageView) findViewById(R.id.imgLinkded);
imgLinkded.setOnClickListener(this);
imgGoogle = (CircleImageView) findViewById(R.id.imgGoogle);
imgGoogle.setOnClickListener(this);
}
private void setColorTheme() {
toolbar.setBackgroundColor(Color.parseColor(sharedObjects.getHeaderColor()));
mIvBackBtn.setColorFilter(Color.parseColor(sharedObjects.getPrimaryColor()));
tvToolbarTitle.setTextColor(Color.parseColor(sharedObjects.getPrimaryColor()));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTvCat.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(sharedObjects.getPrimaryColor())));
btnadd.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(sharedObjects.getPrimaryColor())));
btnview.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor(sharedObjects.getPrimaryColor())));
}
mTvCat.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
btnadd.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
btnview.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
mTvPubDate.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
mTxtcomment.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
mTxtshare.setTextColor(Color.parseColor(sharedObjects.getHeaderColor()));
ivDate.setColorFilter(Color.parseColor(sharedObjects.getHeaderColor()));
ivComment.setColorFilter(Color.parseColor(sharedObjects.getHeaderColor()));
ivShare.setColorFilter(Color.parseColor(sharedObjects.getHeaderColor()));
}
@Override
public void onClick(View v) {
Intent browserIntent;
switch (v.getId()) {
case R.id.ivBackBtn:
// TODO 18/03/16
onBackPressed();
break;
case R.id.imgFacbook:
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + link;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(browserIntent);
break;
case R.id.imgTwitter:
String twittername = "https://twitter.com/intent/tweet?via=shawqealaghbre&text=";
String twitterUrl = "&url=" + link;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(twittername + twitterUrl));
startActivity(browserIntent);
break;
case R.id.imgPin:
String pinUrl = "https://pinterest.com/pin/create/button/?url=" + link;
String media = "&media=" + mImgMain;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(pinUrl + media));
startActivity(browserIntent);
break;
case R.id.imgLinkded:
String LinkedUrl = "http://www.linkedin.com/shareArticle?mini=true&url=" + link;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(LinkedUrl/*+LinkedSummery*/));
startActivity(browserIntent);
break;
case R.id.imgGoogle:
String GplusUrl = "https://plus.google.com/share?url=" + link;
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(GplusUrl));
startActivity(browserIntent);
break;
default:
break;
}
}
private void getNewsById(String parentId, final String newsIdne) {
progressDialog.show();
getRelatedByNewsID(parentId);
Call<JsonElement> call1 = RestClient.post().getNewsByNewsID(newsIdne);
call1.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
progressDialog.dismiss();
if (response.body() != null) {
try {
if (response.isSuccessful()) {
JSONObject jsonObject = new JSONObject(response.body().toString());
if (jsonObject.length() > 0) {
newsId = jsonObject.getString(("id"));
JSONObject title = jsonObject.getJSONObject("title");
titlenews = String.valueOf((Html.fromHtml(title.getString("rendered"))));
mTvTitle.setText(titlenews);
// tvtitle.setText(titlenews);
String date = jsonObject.getString("date");
link = jsonObject.getString("link");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
DateFormat targetFormat = new SimpleDateFormat("MMMM dd yyyy");
String formattedDate = null;
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(date);
System.out.println(date);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("date", formattedDate);
mTvPubDate.setText(formattedDate);
JSONObject ObjEmbeded = jsonObject.getJSONObject("_embedded");
JSONArray wpTermArr = ObjEmbeded.getJSONArray("wp:term");
if (wpTermArr.length() > 0) {
for (int j = 0; j < wpTermArr.length(); j++) {
JSONArray wpTermSubArr = wpTermArr.getJSONArray(j);
if (wpTermSubArr.length() > 0) {
for (int k = 0; k < wpTermSubArr.length(); k++) {
JSONObject wpTerm = wpTermSubArr.getJSONObject(k);
category = String.valueOf(Html.fromHtml(wpTerm.getString("name")));
}
}
}
}
if (ObjEmbeded.has("wp:featuredmedia")) {
JSONArray featureMediaArr = ObjEmbeded.getJSONArray("wp:featuredmedia");
if (featureMediaArr.length() > 0) {
for (int j = 0; j < featureMediaArr.length(); j++) {
JSONObject objectImg = featureMediaArr.getJSONObject(j);
featured_image_link = objectImg.getString("source_url");
}
}
}
if (ObjEmbeded.has("replies")) {
JSONArray repliesArr = ObjEmbeded.getJSONArray("replies");
if(repliesArr.length()>0) {
JSONArray repliesSubArr = repliesArr.getJSONArray(0);
if(repliesSubArr.length()>0){
commentcount = String.valueOf(repliesSubArr.length());
}else{
commentcount = "0";
}
}
}
mTxtcomment.setText(commentcount);
JSONObject content = jsonObject.getJSONObject("content");
Document doc = (Document) Jsoup.parse(content.getString("rendered"));
String html = WebHelper.docToBetterHTML(doc, NewsDetailActivity.this);
String link = "" + newsId + "/" + title;
mWebView.loadDataWithBaseURL(link, html, "text/html", "utf-8", "");
mTvCat.setText(category);
Glide.with(NewsDetailActivity.this).load(featured_image_link)
.thumbnail(0.5f)
.placeholder(R.drawable.bg_img)
.error(R.drawable.bg_img)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL).into(mImgMain);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
try {
String errRess = response.errorBody().source().buffer().toString();
Log.e("ErrRes403String", "- " + errRess);
JSONObject jsonObject = new JSONObject(response.errorBody().source().buffer().readUtf8());
if (jsonObject.length() > 0) {
String id = jsonObject.getString(("id"));
newsId = id;
JSONObject title = jsonObject.getJSONObject("title");
titlenews = String.valueOf((Html.fromHtml(title.getString("rendered"))));
mTvTitle.setText(titlenews);
String date = jsonObject.getString("date");
link = jsonObject.getString("link");
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
DateFormat targetFormat = new SimpleDateFormat("MMMM dd yyyy");
String formattedDate = null;
Date convertedDate = new Date();
try {
convertedDate = dateFormat.parse(date);
System.out.println(date);
formattedDate = targetFormat.format(convertedDate);
} catch (ParseException e) {
e.printStackTrace();
}
Log.e("date", formattedDate);
mTvPubDate.setText(formattedDate);
commentcount = jsonObject.getString("comment_count");
mTxtcomment.setText(commentcount);
video_url = jsonObject.getString("video_url");
JSONObject content = jsonObject.getJSONObject("content");
Document doc = (Document) Jsoup.parse(content.getString("rendered"));
String html = WebHelper.docToBetterHTML(doc, NewsDetailActivity.this);
String link = "" + id + "/" + title;
mWebView.loadDataWithBaseURL(link, html, "text/html", "utf-8", "");
JSONArray array = jsonObject.getJSONArray("category_arr");
for (int j = 0; j < array.length(); j++) {
JSONObject cat = array.getJSONObject(j);
category = String.valueOf(Html.fromHtml(cat.getString("name")));
}
String id1 = null;
String titlenew = null;
mTvCat.setText(category);
featured_image_link = jsonObject.getString("featured_image_link");
Glide.with(NewsDetailActivity.this).load(featured_image_link)
.thumbnail(0.5f)
.placeholder(R.drawable.bg_img)
.error(R.drawable.bg_img)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL).into(mImgMain);
}
} catch (JSONException e) {
e.printStackTrace();
}
Toast.makeText(NewsDetailActivity.this, "no dataaaaa", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(NewsDetailActivity.this, getString(R.string.something_went_wrong), Toast.LENGTH_SHORT).show();
}
});
}
private void getRelatedByNewsID(String parentId) {
relatedListId = new String[4];
final String[] title = new String[4];
Call<JsonElement> call1 = RestClient.post().getNewsByCatID(parentId);
call1.enqueue(new Callback<JsonElement>() {
@Override
public void onResponse(Call<JsonElement> call, Response<JsonElement> response) {
Log.e("News", response.body().toString());
try {
if (response.isSuccessful()) {
JSONArray jsonArr = new JSONArray(response.body().toString());
if (jsonArr.length() > 0) {
customtlist.clear();
int length = jsonArr.length();
if (length < 3)
length = jsonArr.length();
else
length = 3;
// int count = 0;
for (int i = 0; i < length; i++) {
JSONObject json2 = jsonArr.getJSONObject(i);
JSONObject objtitle = json2.getJSONObject("title");
CustomDataBean customDataBean = new CustomDataBean();
customDataBean.setId(json2.getString("id"));
customDataBean.setPost_title(objtitle.getString("rendered"));
customDataBean.setCount(String.valueOf(i + 1));
customtlist.add(customDataBean);
}
}
bindRelatedCategoryAdapternews();
Log.e("Cat size : ", customtlist.size() + "");
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
nestedScrollView.scrollTo(0, 0);
}
}, SPLASH_TIME_OUT);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<JsonElement> call, Throwable t) {
Toast.makeText(NewsDetailActivity.this, getString(R.string.something_went_wrong), Toast.LENGTH_SHORT).show();
}
});
}
private void bindRelatedCategoryAdapternews() {
if (customtlist.size() > 0) {
coustomlistAdapter = new CoustomlistAdapter(NewsDetailActivity.this, customtlist);
coustomlistAdapter.setRelatedNewsSelectedListner(this);
rvCustom.setAdapter(coustomlistAdapter);
}
}
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
@Override
public void setOnRelatedNewsSelectedListner(int position, CustomDataBean customDataBean) {
getNewsById(parentId, customDataBean.getId());
}
@Override
protected void onResume() {
super.onResume();
getNewsById(parentId, newsId);
}
private class Browser_home extends WebViewClient {
Browser_home() {
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
setTitle(view.getTitle());
super.onPageFinished(view, url);
}
}
private class MyChrome extends WebChromeClient {
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
protected FrameLayout mFullscreenContainer;
private int mOriginalOrientation;
private int mOriginalSystemUiVisibility;
MyChrome() {}
public Bitmap getDefaultVideoPoster()
{
if (mCustomView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
public void onHideCustomView()
{
((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
this.mCustomView = null;
getWindow().getDecorView().setSystemUiVisibility(this.mOriginalSystemUiVisibility);
setRequestedOrientation(this.mOriginalOrientation);
this.mCustomViewCallback.onCustomViewHidden();
this.mCustomViewCallback = null;
}
public void onShowCustomView(View paramView, WebChromeClient.CustomViewCallback paramCustomViewCallback)
{
if (this.mCustomView != null)
{
onHideCustomView();
return;
}
this.mCustomView = paramView;
this.mOriginalSystemUiVisibility = getWindow().getDecorView().getSystemUiVisibility();
this.mOriginalOrientation = getRequestedOrientation();
this.mCustomViewCallback = paramCustomViewCallback;
((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
getWindow().getDecorView().setSystemUiVisibility(3846);
}
最佳答案
您的问题是因为您在 onShowCustomView 方法中添加的新 FrameLayout 没有背景色,默认情况下是透明。 您只需更改新框架布局的背景颜色即可解决您的问题。
在onShowCustomView方法中,将framelayout的背景颜色更改为黑色
((FrameLayout)getWindow().getDecorView()).addView(this.mCustomView, new FrameLayout.LayoutParams(-1, -1));
this.mCustomView.setBackgroundColor(Color.BLACK); //background color for framelayout
getWindow().getDecorView().setSystemUiVisibility(3846);
但我建议您不要使用 chrome 客户端,因为您有很多不必要的语句,这些语句对于您想要实现的目标没有任何用处。
public class chromeWebView extends WebChromeClient {
View customView;
WebChromeClient.CustomViewCallback mCustomViewCallback;
public Bitmap getDefaultVideoPoster()
{
if (customView == null) {
return null;
}
return BitmapFactory.decodeResource(getApplicationContext().getResources(), 2130837573);
}
public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
if (this.customView != null) {
onHideCustomView();
return;
}
this.customView = view;
((FrameLayout)getWindow().getDecorView()).addView(this.customView, new FrameLayout.LayoutParams(-1, -1));
this.customView.setBackgroundColor(Color.BLACK);
this.mCustomViewCallback = callback;
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);
}
public void onHideCustomView() {
((FrameLayout)getWindow().getDecorView()).removeView(this.mCustomView);
this.customView = null;
this.mCustomViewCallback.onCustomViewHidden();
getWindow().getDecorView().setSystemUiVisibility(~View.SYSTEM_UI_FLAG_FULLSCREEN);
}
}
使用上面的chrome客户端它也可以解决你的问题,而无需使用任何不必要的语句。
关于java - 在全屏模式下在 webview 中播放视频时隐藏后台 Activity 的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57648421/