Skip to content

Commit

Permalink
修复web控件间距问题
Browse files Browse the repository at this point in the history
  • Loading branch information
SunRt233 committed Oct 9, 2022
1 parent 34def2f commit b5eee9d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 24 deletions.
1 change: 1 addition & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions app/src/main/java/top/xfunny/servermap/MetroActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;

import top.xfunny.servermap.ui.activity.WebActivity;

Expand All @@ -15,4 +16,11 @@ protected void onCreate(Bundle savedInstanceState) {
getWebSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
loadUrl("http://112.124.52.20:8888");
}

@Override
protected void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.evaluateJavascript("var a = document.getElementById('search')",(r) -> {});
view.evaluateJavascript("a.style.marginTop = (parseInt(window.getComputedStyle(a).marginTop) + ajs.getStatusBarHeight()) + 'px'",(r) -> {});
}
}
11 changes: 8 additions & 3 deletions app/src/main/java/top/xfunny/servermap/OverworldActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.Toast;

import top.xfunny.servermap.ui.activity.WebActivity;
Expand All @@ -16,9 +17,13 @@ protected void onCreate(Bundle savedInstanceState) {

getWebSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
loadUrl("http://112.124.52.20:5000");
getWebView().evaluateJavascript("ajs.getStatusBarHeight()",(result) -> {
Toast.makeText(this,result,Toast.LENGTH_SHORT).show();
});

}

@Override
protected void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.evaluateJavascript("var doms = [document.getElementsByClassName('ol-zoom')[0], document.getElementsByClassName('ol-mouse-position')[0]];",(result) -> {});
view.evaluateJavascript("doms.forEach(e => { e.style.marginTop = ajs.getStatusBarHeight()+\"px\" });",(result) -> {});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.webkit.JavascriptInterface;
Expand Down Expand Up @@ -50,6 +52,7 @@ public void initWebView() {

mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
WebView.setWebContentsDebuggingEnabled(true);
mWebView.addJavascriptInterface(new JSInterface() {
}, "ajs");

Expand Down Expand Up @@ -86,6 +89,8 @@ public void onAnimationEnd(Animator animation) {
mPgBar.setVisibility(View.GONE);
}
});

WebActivity.this.onPageFinished(view, url);
}
});
}
Expand All @@ -94,6 +99,8 @@ public void loadUrl(String url) {
if (mWebView != null) mWebView.loadUrl(url);
}

protected void onPageFinished(WebView view, String url){}

public WebSettings getWebSettings() {
return mWebSettings;
}
Expand All @@ -107,14 +114,15 @@ public class JSInterface {

@JavascriptInterface
public int getStatusBarHeight() {
int height = 0;
int resourceId = getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
height = getApplicationContext().getResources().getDimensionPixelSize(resourceId);
}
// int height = 0;
// int resourceId = getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
// if (resourceId > 0) {
// height = getApplicationContext().getResources().getDimensionPixelSize(resourceId);
// }

return height;
DisplayMetrics dm = WebActivity.this.getResources().getDisplayMetrics();

return (int) (12f * (dm.densityDpi / 160f));
}
}
}

10 changes: 4 additions & 6 deletions app/src/main/res/layout/activity_about.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginTop="0dp"
android:orientation="vertical"
android:rotationY="6">
android:orientation="vertical">


<TextView
Expand All @@ -14,24 +13,23 @@
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="ServerMap"
android:textColor="@color/material_dynamic_neutral30"
android:textSize="165px" />
android:textSize="165dp" />

<TextView
android:id="@+id/author"
android:layout_width="200dp"
android:layout_height="100dp"
android:layout_marginLeft="5dp"
android:text="WebView:XFunny \nUI框架:SunRt233"
android:textSize="50px" />
android:textSize="50dp" />

<TextView
android:id="@+id/server"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="UrbanConstruction服务器专用"
android:textSize="50px" />
android:textSize="50sp" />

<WebView
android:id="@+id/webview1"
Expand Down
12 changes: 6 additions & 6 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,15 @@
android:clickable="true"
android:focusable="true"
android:foreground="?attr/selectableItemBackground"
app:cardBackgroundColor="@color/material_dynamic_neutral40"
app:cardBackgroundColor="#5c5f61"
app:cardCornerRadius="@dimen/card_corner_radius">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left">
android:gravity="center|start">
<TextView
android:paddingLeft="24dp"
android:paddingStart="24dp"
android:textSize="16sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
Expand All @@ -88,10 +88,10 @@
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|left">
android:gravity="center|start">
<TextView
android:paddingLeft="24dp"
android:textSize="16dp"
android:paddingStart="24dp"
android:textSize="16sp"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Expand Down
2 changes: 0 additions & 2 deletions app/src/main/res/layout/activity_web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
android:background="?android:attr/colorBackground">

<androidx.core.widget.ContentLoadingProgressBar
android:id="@+id/progressBar"
android:indeterminateTint="@color/material_dynamic_primary30"
style="@style/Widget.AppCompat.ProgressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Expand Down

0 comments on commit b5eee9d

Please sign in to comment.