第一 在使用XScrollView布局是,无法在该布局.xml文件,放置内容布局控件,假如放置了会报错

<com.markmao.pulltorefresh.widget.XScrollViewandroid:id="@+id/scroll_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/page_top"android:fillViewport="true"android:scrollbars="none"></com.markmao.pulltorefresh.widget.XScrollView>

XScrollView,通过看下面的代码你会发现该控件在初始化时已经去动态添加了一个子控件,假如你再去放置内容布局肯定会报错,因为android针对ScrollView的默认设置是只允许包含唯一子空间

publicclassXScrollViewextendsScrollViewimplementsOnScrollListener{privateLinearLayoutmLayout;privateLinearLayoutmContentLayout;publicXScrollView(Contextcontext){super(context);initWithContext(context);}publicXScrollView(Contextcontext,AttributeSetattrs){super(context,attrs);initWithContext(context);}publicXScrollView(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);initWithContext(context);}privatevoidinitWithContext(Contextcontext){mLayout=(LinearLayout)View.inflate(context,R.layout.vw_xscrollview_layout,null);mContentLayout=(LinearLayout)mLayout.findViewById(R.id.content_layout);this.addView(mLayout);}

R.layout.vw_xscrollview_layout 该布局文件的内部,头部与顶部的咱们先不用管,就看中间的,ID值为 content_layout,默认我们的自定义布局是放置嵌套在其中的

<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/header_layout"android:layout_gravity="center_horizontal|top"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"/><LinearLayoutandroid:id="@+id/content_layout"android:layout_gravity="center"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="vertical"/><LinearLayoutandroid:id="@+id/footer_layout"android:layout_gravity="center_horizontal|bottom"android:layout_width="match_parent"android:layout_height="wrap_content"android:tag="ttttt"android:orientation="vertical"/></LinearLayout>

publicvoidsetContentView(ViewGroupcontent){if(mLayout==null)return;if(mContentLayout==null)mContentLayout=(LinearLayout)mLayout.findViewById(R.id.content_layout);if(mContentLayout.getChildCount()>0)mContentLayout.removeAllViews();mContentLayout.addView(content);}publicvoidsetView(Viewcontent){if(mLayout==null)return;if(mContentLayout==null)mContentLayout=(LinearLayout)mLayout.findViewById(R.id.content_layout);mContentLayout.addView(content);}

外部引入 ,设置内容的函数有两个,setContentView,setView

Viewcontent=LayoutInflater.from(this).inflate(R.layout.vw_scroll_view_content,null);scrollview.setContentView()content;

下面的布局文件还是用一个使用XScrollView的布局文件,内容布局也放置在该文件中,但是跟XScrollView就不是父子的关系,而是同级的,ID xcollview_content,就是内容布局,接下来就看代码的

<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/test_parent"android:layout_width="match_parent"android:layout_height="match_parent"><com.markmao.pulltorefresh.widget.XScrollViewandroid:id="@+id/scroll_view"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/page_top"android:fillViewport="true"android:scrollbars="none"></com.markmao.pulltorefresh.widget.XScrollView><LinearLayoutandroid:id="@+id/xcollview_content"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@id/scroll_view"android:background="@color/transparent"android:orientation="vertical"><ListViewandroid:id="@+id/content_list"android:layout_width="match_parent"android:layout_height="match_parent"android:cacheColorHint="#00000000"android:scrollbars="none"/></LinearLayout></RelativeLayout>

代码变动,XScrollView中新增函数 ,需要注意的一个空间它只允许有一个父控件,到此步就结束了第一个问题

publicvoidsetDView(Viewcontent){if(mLayout==null)return;if(mContentLayout==null)mContentLayout=(LinearLayout)mLayout.findViewById(R.id.content_layout);ViewParentparent=this.getParent();if(parentinstanceofRelativeLayout){RelativeLayoutr_parent=(RelativeLayout)parent;r_parent.removeView(content);}if(parentinstanceofLinearLayout){LinearLayoutl_parent=(LinearLayout)parent;l_parent.removeView(content);}mContentLayout.addView(content);}


以上!另外对APP进行全方位的检测,我都会用这个:www.ineice.com


更多相关文章

  1. Android(安卓)广播获取短信内容
  2. Android(安卓)layout xml总结
  3. 悬浮顶层控件不受ScrollView影响
  4. Android(安卓)AlertDialog有EditText无法弹出输入法的解决
  5. tabhost放到底部显示
  6. Android实现画虚线的控件
  7. android mapview 学习总结
  8. 安卓开发10:常用控件-常用表单级别控件
  9. android 单元测试之dialog测试链接整理

随机推荐

  1. Android Display System
  2. android关闭或开启移动网络数据(关闭后,设
  3. Handler简要介绍
  4. android的volley框架
  5. android 编译过程及错误处理
  6. android dialog用法总结
  7. Android中Textview显示带html文本
  8. Android核心基础(十)
  9. Android 四大组件之 Activity(1)--生命周
  10. Android单击显示contextMenu