我如何能实现像&QUOT铬,自动隐藏导航"我的Android应用程序? [英] How can I implement a chrome like "auto-hide navigation" for my Android app?
问题描述
在Chrome浏览器,地址栏会被隐藏/显示,当用户扫描上/下的内容。
我可以实现类似的逻辑,以我的应用程序?
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID /容器
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=。MainActivity
工具:忽略=MergeRootFrame>
<的WebView
机器人:ID =@ + ID / my_webview
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>
< /的FrameLayout>
我不知道是否有什么我可以用下面的code执行,从而使动作条将被隐藏/显示,当用户刷卡向上/向下web视图。
的WebView web视图=(web视图)findViewById(R.id.my_webview);
webView.setOnTouchListener(新View.OnTouchListener(){
公共布尔onTouch(视图V,MotionEvent事件){
webView.onTouchEvent(事件);
}
});
首先,你需要有布局 alignParentBottom 和 match_parent 的WebView有的酒吧在顶部:
< XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:后台=#FFFF>
<的WebView
机器人:ID =@ + ID / web视图
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:layout_alignParentBottom =真/>
< RelativeLayout的
机器人:ID =@ + ID /动作条
机器人:layout_width =match_parent
机器人:layout_height =50dp>
<的TextView
机器人:ID =@ + ID / tvTitle
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerInParent =真
机器人:文本=TextView的/>
< / RelativeLayout的>
< / RelativeLayout的>
然后,你需要code:
1)声明并设置一些内在价值和调整的WebView使其下面我们的吧:
私人诠释baseHeight;
私人诠释webViewOnTouchHeight;
私人诠释barHeight;
私人浮动heightChange;
私人浮动startEventY;
barHeight = actionBar.getMeasuredHeight();
baseHeight = getView()getMeasuredHeight()。
。webView.getLayoutParams()高度= webView.getMeasuredHeight() - barHeight;
webView.requestLayout();
webView.setOnTouchListener(听众);
2)创建大小调整方法。在这里,我们检查新的高度与屏幕顶部的限制和杆高度
私人布尔resizeView(浮动三角洲){
heightChange =增量;
INT newHeight =(INT)(webViewOnTouchHeight - 三角洲);
如果(newHeight> baseHeight){//滚动过顶
如果(webView.getLayoutParams()高度< baseHeight){
。webView.getLayoutParams()高度= baseHeight;
webView.requestLayout();
返回true;
}
}否则如果(newHeight< baseHeight - barHeight){//下面的滚动条
如果(webView.getLayoutParams()高度> baseHeight - barHeight。){
webView.getLayoutParams()高度= baseHeight - barHeight。
webView.requestLayout();
返回true;
}
}其他{//之间的顶部和酒吧滚动
。webView.getLayoutParams()身高=(INT)(webViewOnTouchHeight - 三角洲);
webView.requestLayout();
返回true;
}
返回false;
}
3)创建自定义onTouch监听器那里,我们将调整的WebView和更改Y值我们吧
私人OnTouchListener监听器=新OnTouchListener(){
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
开关(event.getAction()及MotionEvent.ACTION_MASK){
案例MotionEvent.ACTION_DOWN:
startEventY = event.getY();
heightChange = 0;
webViewOnTouchHeight = webView.getLayoutParams()的高度。
打破;
案例MotionEvent.ACTION_MOVE:
浮动三角洲=(event.getY()+ heightChange)-startEventY;
布尔heigthChanged = resizeView(增量);
如果(heigthChanged){
actionBar.setTranslationY(baseHeight - webView.getLayoutParams()高度 - barHeight);
}
}
返回false;
}
};
In Chrome, the address bar will be hidden/shown when user swipes up/down the content.
Can I implement the similar logic to my app?
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame">
<WebView
android:id="@+id/my_webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
I wonder if there is anything I can do by the following code, so that the ActionBar will be hidden/shown when user swipe up/down the webView.
WebView webView = (WebView) findViewById(R.id.my_webview);
webView.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch (View v, MotionEvent event) {
webView.onTouchEvent(event);
}
});
First you need layout with alignParentBottom and match_parent WebView and some bar at top:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff" >
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true" />
<RelativeLayout
android:id="@+id/actionBar"
android:layout_width="match_parent"
android:layout_height="50dp" >
<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Then you need code:
1) Declare and set some internal values and resize WebView to make it below our bar:
private int baseHeight;
private int webViewOnTouchHeight;
private int barHeight;
private float heightChange;
private float startEventY;
barHeight = actionBar.getMeasuredHeight();
baseHeight = getView().getMeasuredHeight();
webView.getLayoutParams().height = webView.getMeasuredHeight() - barHeight;
webView.requestLayout();
webView.setOnTouchListener(listener);
2) Create resize method. Here we check new height with limits of screen top and bar height
private boolean resizeView(float delta) {
heightChange = delta;
int newHeight = (int)(webViewOnTouchHeight - delta);
if (newHeight > baseHeight){ // scroll over top
if (webView.getLayoutParams().height < baseHeight){
webView.getLayoutParams().height = baseHeight;
webView.requestLayout();
return true;
}
}else if (newHeight < baseHeight - barHeight){ // scroll below bar
if (webView.getLayoutParams().height > baseHeight - barHeight){
webView.getLayoutParams().height = baseHeight - barHeight;
webView.requestLayout();
return true;
}
} else { // scroll between top and bar
webView.getLayoutParams().height = (int)(webViewOnTouchHeight - delta);
webView.requestLayout();
return true;
}
return false;
}
3) Create custom onTouch listener where we will resize WebView and change Y-value of our bar
private OnTouchListener listener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startEventY = event.getY();
heightChange = 0;
webViewOnTouchHeight = webView.getLayoutParams().height;
break;
case MotionEvent.ACTION_MOVE:
float delta = (event.getY()+heightChange)-startEventY;
boolean heigthChanged = resizeView(delta);
if (heigthChanged){
actionBar.setTranslationY(baseHeight - webView.getLayoutParams().height - barHeight);
}
}
return false;
}
};
这篇关于我如何能实现像&QUOT铬,自动隐藏导航&QUOT;我的Android应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!