Android之ScrollView

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Monkey_LZL/article/details/53969122

关于android中ScrollView的用法之一:

设置底部控件view随软件键盘的弹出而自动上移,使用ScrollView,具体代码为:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff" >


    <ScrollView
        android:id="@+id/sv1"
        android:layout_alignParentTop="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/sv2"
        android:fillViewport="true"
        android:scrollbars="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:focusable="true"
            android:focusableInTouchMode="true">
            <requestFocus />


            <ImageView
                android:id="@+id/c_img"
                android:layout_width="300dp"
                android:layout_height="500dp"
                android:visibility="gone" >
            </ImageView>


            <VideoView
                android:id="@+id/c_video"
                android:layout_width="300dp"
                android:layout_height="500dp"
                android:visibility="gone" >
            </VideoView>


            <EditText
                android:id="@+id/ettext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@null"
                android:gravity="top"
                android:hint="记录精彩瞬间"
                android:textColorHint="#000000" />
        </LinearLayout>
    </ScrollView>


    <ScrollView
        android:id="@+id/sv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:fillViewport="true"
        android:scrollbars="vertical" >


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >


            <Button
                android:id="@+id/save"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="保存" >
            </Button>


            <Button
                android:id="@+id/share"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="分享" >
            </Button>


            <Button
                android:id="@+id/delete"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="取消" >
            </Button>
        </LinearLayout>
    </ScrollView>


</RelativeLayout>


达到的效果图为: 

                                                                                    

其中android:layout_alignParentTop="true"语句可以使的Android中hint的字显示在最上边,而不是紧贴button

注意RelativeLayout中各个布局之间的关系。

android:layout_below 在某元素的下方,这种属性语句对应的是同一级的布局,LinearLayout对应LinearLayout,而LinearLayout对应ListView则会报错。同理android:layout_alignParentTop语句对应的是RelativeLayout下面紧跟的两个ScrollView,而越级设置LinearLayout是无用的。

android:focusable="true"
android:focusableInTouchMode="true">
<requestFocus />

作用是可以通过进入EditText,触摸屏幕来自动对焦。

猜你喜欢

转载自blog.csdn.net/Monkey_LZL/article/details/53969122