Android的界面布局中为我们提供了ScrollView组件,即当页面的信息过多而超过屏幕时,产生滑动效果来查看页面内容,但是使用时需要注意几点:
1,ScrollView一般与LinearLayout同时使用,之所以使LinearLayout是因为LinearLayout本身的特性,LinearLayout会根据组件来扩展自身的高度,这样在使用ScrollView的时候,会保证所有的内容都能显示出来,而RelativeLayout在使用的时候就会麻烦很多,因为相对布局中,如果以父边框为基准,使用margin来调控距离,不会增加RelativeLayout自身的高度,反而会将子控件的控件压缩,所以在ScrollView与RelativeLayout相结合使用的时候,可以使用padding来调控组件的距离,用扩大组件的宽度或者高度的方法来实现间距的效果。
2,当ScrollView没有fillViewport=“true”时,里面的元素(比如LinearLayout)会按照wrap_content来计算(不论它是否设了” match _parent”) 这个android:fillViewport很重要。
<ScrollView
android:fillViewport="true"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3">
<!--意见-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="10dp">
<LinearLayout
android:id="@+id/ll_idea_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_resopinion"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/shape_advise1"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingTop="15dp"
android:text="@string/document_idea_resopinion"
android:textAppearance="@style/common_text_size"
android:textColor="@color/font_gray"/>
<cn.gov.sz.oa.widget.NoScrollListView
android:visibility="gone"
android:id="@+id/pull_resopinion"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginTop="15dp"
android:id="@+id/tv_leader_idea"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/shape_advise1"
android:gravity="center_vertical"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingTop="15dp"
android:text="@string/document_title_idea"
android:textAppearance="@style/common_text_size"
android:textColor="@color/font_gray"/>
<cn.gov.sz.oa.widget.NoScrollListView
android:id="@+id/lv_leader_idea"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<include layout="@layout/content_empty"/>
</RelativeLayout>
</ScrollView>
3,如果ScrollView嵌套的布局中包括有自带滚动的控件,会将自带滚动上面的一些控件挤出屏幕,原因是这些控件失去了焦点,解决方案就是给最顶端的控件设置获取焦点。
view.setFocusable(true);
view.setFocusableInTouchMode(true);
view.requestFocus();