我有一个创建了一个组合控件,然后想让这个组合控件在页面内随意拖动实现返回顶部的功能,然后FloatingView继承了这个组合控件后,使用报运行时异常,请问我该怎么修改?谢谢
下面是组合控件:
public class BackTopView extends RelativeLayout {
private TextView text_view;
private View rootView;
private String back_text;
public BackTopView(Context context) {
super(context);
init();
}
public BackTopView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
back_text=attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","setting_text");
text_view.setText(back_text);
}
public BackTopView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
rootView=View.inflate(getContext(), R.layout.back_top_layout,this);
text_view=rootView.findViewById(R.id.back_text);
}
public void setText(String text){
text_view.setText(text);
}
}
下面是悬浮实现
public class FloatingView extends BackTopView {
int startX;
int startY;
int left;
int top;
int[] temp = new int[]{ 200, 200 };
public FloatingView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean isMove = false;
int x = (int) event.getRawX();
int y = (int) event.getRawY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN: // touch down so check if the
startX = x;
startY = y;
temp[0] = (int) event.getX();
temp[1] = y - getTop();
break;
case MotionEvent.ACTION_MOVE: // touch drag with the ball
left = x - temp[0];
top = y - temp[1];
if(left < 0){//控制左边界不超出
left = 0;
}
layout(left, top, left + getWidth(),top + getHeight());//自由拖拽
break;
case MotionEvent.ACTION_UP:
if (Math.abs(x - startX) > 2 || Math.abs(y - startY) > 2){//判断是否移动,再一定范围内不算是移动,解决触发事件冲突
//将最后拖拽的位置定下来,否则页面刷新渲染后按钮会自动回到初始位置
//注意父容器
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
lp.setMargins(left, top,0,0);
setLayoutParams(lp);
//确定是拖拽
isMove = true;
}
break;
}
return isMove ? true : super.onTouchEvent(event);
}
}
引用到的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"
android:id="@+id/first_layout"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AssetsActivity"
android:orientation="vertical"
android:clickable="true"
android:focusableInTouchMode="true">
<RelativeLayout
android:id="@+id/edit_relativew"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/edit_frame_shape">
<EditText
android:id="@+id/assets_edit"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="15dp"
android:background="@null"
android:hint=" 搜索设备"
android:imeOptions="actionSearch"
android:singleLine="true"></EditText>
<ImageButton
android:id="@+id/assets_btu"
android:layout_alignRight="@id/assets_edit"
android:layout_centerVertical="true"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center"
android:background="@drawable/select">
</ImageButton>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/edit_relativew"
android:layout_marginTop="10dp"
android:orientation="vertical">
<com.zxkj.e_delivermaintain.Adapter.LoadListView
android:id="@+id/assets_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="@null">
</com.zxkj.e_delivermaintain.Adapter.LoadListView>
</RelativeLayout>
<include
android:id="@+id/emptyview"
layout="@layout/list_empty"/>
<com.zxkj.e_delivermaintain.Adapter.FloatingView
android:id="@+id/back_top"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
</com.zxkj.e_delivermaintain.Adapter.FloatingView>
</RelativeLayout>
报错内容
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zxkj.e_delivermaintain/com.zxkj.e_delivermaintain.AssetsActivity}: android.view.InflateException: Binary XML file line #68: Binary XML file line #68: Error inflating class com.zxkj.e_delivermaintain.Adapter.FloatingView
谢谢你的帮助!!