虽然工作好几年了,但还是初次写博客,除了想记录日常工作中收获的内容以外,也希望能够给遇到相同问题的你们有所帮助,写的不好还请见谅!
以下是一个悬浮输入框页面的实现,同其它类似demo 的主要区别是,虽然复用同一个布局,但是能够改变悬浮页面的背景和颜色,能够与原本的输入框区分开来
1 布局文件,以下是需要悬浮的内容,当然也是原本需要显示的内容
<RelativeLayout
android:id="@+id/serch_lay"
android:layout_width="fill_parent"
android:layout_height="57dp"
android:layout_marginTop="30dp">
<View
android:id="@+id/sch_viewwhite"
android:layout_width="fill_parent"
android:layout_height="7dp"
android:background="@color/white" />
<RelativeLayout
android:id="@+id/serch_input"
android:layout_width="fill_parent"
android:layout_height="57dp"
android:background="@color/white">
<EditText
android:id="@+id/searchText"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:textColor="#333333"
android:paddingLeft="60dp"
android:paddingRight="55dp"
android:singleLine="true"
android:hint="@string/search_tips"
android:textColorHint="@color/detail_share"
android:background="@color/sch_edit"
android:textSize="15dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="7dp"
android:gravity="center_vertical"
android:imeOptions="actionSearch"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginRight="25dp"
android:layout_marginTop="7dp">
<ImageButton
android:id="@+id/searchButton"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="@drawable/search_select"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
<ImageButton
android:id="@+id/cancelsearch"
android:layout_width="15dp"
android:layout_height="15dp"
android:background="@drawable/cancelsearch_normal"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/searchButton"
android:layout_marginRight="20dp"
android:clickable="true"
android:visibility="gone"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
2 布局复用, 这步同第一步的区别大家可以看看,父布局是一模一样,当然因为是悬浮高度肯定和页面顶层间距为0,中间的serch_input这一层相对布局就是复用的部分,随滑动间距动态增删
<RelativeLayout
android:id="@+id/serch_lay2"
android:layout_width="fill_parent"
android:layout_height="57dp"
android:visibility="gone">
<View
android:layout_width="fill_parent"
android:layout_height="7dp"
android:background="@color/white" />
</RelativeLayout>
3 核心代码
sch_scrollView.setOnScrollListener(new MyScrollView.OnScrollListener() {
@Override
public void onScrolled() {
int y = sch_scrollView.getScrollY();
if(layoutTop == 0){
layoutTop = serch_lay.getBottom() - serch_lay.getHeight();
System.out.println("sch_scrollView layoutTop: " + layoutTop);
}
if(y >= layoutTop){
if (lay_edit.getParent()!=serch_lay2) {
if(serch_lay2.getVisibility() != View.VISIBLE){
serch_lay2.setVisibility(View.VISIBLE);
}
serch_lay.removeView(lay_edit);
serch_lay2.addView(lay_edit);
searchText.setBackgroundResource(R.color.white);
lay_edit.setBackgroundResource(R.drawable.serch_lay);
}
}else{
if (lay_edit.getParent()!=serch_lay) {
serch_lay2.removeView(lay_edit);
serch_lay.addView(lay_edit);
if(serch_lay2.getVisibility() != View.GONE){
serch_lay2.setVisibility(View.GONE);
}
searchText.setBackgroundResource(R.color.sch_edit);
lay_edit.setBackgroundResource(R.color.white);
}
}
}
});
4 源码下载地址
http://download.csdn.net/detail/zs20082012/9598811