当前位置 : 主页 > 网页制作 > xml >

悬停view的使用

来源:互联网 收集:自由互联 发布时间:2021-06-13
首先是布局资源 activity_main.xml ? xml version= "1.0" encoding= "utf-8" ? RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" xmlns: tools = "http://sche

首先是布局资源

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">



<com.example.stickview.MyScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#ffff00"
/>
<TextView
android:id="@+id/t2"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:text="我是全局悬浮view"
/>

<TextView
android:layout_width="match_parent"
android:layout_height="600dp"
android:background="@color/colorPrimaryDark"
/>
</LinearLayout>
</com.example.stickview.MyScrollView>
<TextView
android:id="@+id/t1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@color/colorAccent"
android:text="我是假全局悬浮view"
android:visibility="gone"
/>
</RelativeLayout>






MainActivity代码
package com.example.stickview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

MyScrollView scroll;
TextView t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scroll = (MyScrollView)findViewById(R.id.scroll);
t1 = (TextView)findViewById(R.id.t1);
t2 = (TextView)findViewById(R.id.t2);
scroll.setView(t1,t2);
}
}




自定义ScrollView代码
package com.example.stickview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ScrollView;
import android.widget.TextView;


public class MyScrollView extends ScrollView {

private static StopCall stopCall;
Context ctx;
TextView t1,t2;
//ScrollView向上滑动到顶部的距离
private int upH;

public MyScrollView(Context context) {

super(context);

}

public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
this.ctx = context;
//赋值:300很重要,这个值是顶部2上面的高度,也就是本例中图片的高度
upH = dpToPx(ctx,200);//单位是dp
}

public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);

}


public static void setCallback(StopCall c){
stopCall =c ;
}


@Override protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
super.onScrollChanged(l, t, oldl, oldt);
if(t>upH) {
stopSlide(true);
}
else{
stopSlide(false);
}

}

/**
* 接口实现,可不可就
*/
public void stopSlide(boolean isStop){
if (isStop) {
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.GONE);
} else {
t2.setVisibility(View.VISIBLE);
t1.setVisibility(View.GONE);
}
}


public interface StopCall {

public void stopSlide(boolean isStop);
}


public static int dpToPx(Context context,float dpValue) {//dp转换为px
float scale=context.getResources().getDisplayMetrics().density;//获得当前屏幕密度
return (int)(dpValue*scale+0.5f);
}


public void setView(TextView t1,TextView t2)
{
this.t1 = t1;
this.t2 = t2; } }
上一篇:log4j2.xml.note
下一篇:XML &lt;![CDATA[]]&gt;
网友评论