android 悬浮框的实现

虽然工作好几年了,但还是初次写博客,除了想记录日常工作中收获的内容以外,也希望能够给遇到相同问题的你们有所帮助,写的不好还请见谅!

以下是一个悬浮输入框页面的实现,同其它类似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


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值