安卓开发中ScrollView不能用RelativeLayout的解决方案

RelativeLayout的意义:

布局各个部件的相对布局。使得界面空间合理利用。

一、ScrollView局限:

滑动的只能是linearlayout,甚至整个布局都不能有RelativeLayout。这使得让人觉得ScrollView控件有点鸡肋。其实不然..........

linearlayout跟RelativeLayout布局在界面上来看只是空间间隔的区别,而在liearlayout中有一个<View/>能占用空间,单单用linearlayout要达到    RelativeLayout的效果,可以用<View/>来实现。

二、View用法

<View

android:layout_width="fill_parent"

       android:layout_height="10sp"

/>

三、例子

下面的界面

1)用RelativeLayout实现非常容易实现

RelativeLayout代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    tools:context=".ClientActivity" >
    <ImageView
        android:id="@+id/clientinfo_headpicture"
        android:layout_width="90sp"
        android:layout_height="90sp"
        android:background="@drawable/client_pic_back"
        android:src="@drawable/client_pic_src" />
    <TextView
        android:id="@+id/client_tel"
        android:layout_width="match_parent"
        android:layout_height="30sp"
        android:layout_alignBottom="@+id/clientinfo_headpicture"
        android:layout_toRightOf="@+id/clientinfo_heaspicture"
        android:background="@color/black"
        android:ems="12"
        android:singleLine="true"
        android:text="电话号码"
        android:textColor="@color/white" />
    <Button
        android:id="@+id/client_tel_edit"
        android:layout_width="50sp"
        android:layout_height="30sp"
        android:layout_above="@+id/client_tel"
        android:layout_alignParentRight="true"
        android:background="@drawable/cliebt_button_edittel"
        android:text="编辑"
        android:textColor="@color/blue" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="30sp"
        android:layout_alignLeft="@+id/client_tel"
        android:layout_alignParentTop="true"
        android:background="@color/black"
        android:singleLine="true"
        android:text="用户名"
        android:textColor="@color/white"
        android:textSize="24sp" />
    <View
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="10sp"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/clientinfo_headpicture"
        android:background="@color/blue" />
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/view1"
        android:background="@drawable/client_moreinfo_back" >
        <Button
            android:id="@+id/client_myaddress"
            android:layout_width="50sp"
            android:layout_height="30sp"
            android:background="@drawable/client_address" />
        <TextView
            android:id="@+id/client_myaddress_text"
            android:layout_width="wrap_content"
            android:layout_height="30sp"
            android:layout_alignBaseline="@+id/client_myaddress"
            android:layout_alignBottom="@+id/client_myaddress"
            android:layout_toRightOf="@+id/client_myaddress"
            android:text="我的地址"
            android:textColor="@color/blue"
            android:textSize="24sp" />
        <View
            android:id="@+id/client_myaddress_div"
            android:layout_width="fill_parent"
            android:layout_height="1sp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/client_myaddress"
            android:background="@color/blue" />
        <TextView
            android:id="@+id/client_myaddress_addtext"
            android:layout_width="160sp"
            android:layout_height="50sp"
            android:layout_alignLeft="@+id/client_myaddress_div"
            android:layout_below="@+id/client_myaddress_div"
            android:layout_marginLeft="14sp"
            android:text="31栋127寝室电话18607060000"
            android:textColor="@color/blue" />
        <Button
            android:id="@+id/client_myaddress_edit"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:layout_alignRight="@+id/client_myaddress_div"
            android:layout_alignTop="@+id/client_myaddress_addtext"
            android:background="@drawable/client_edit" />
        <View
            android:id="@+id/client_buyhistory_div"
            android:layout_width="fill_parent"
            android:layout_height="5sp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/client_myaddress_addtext"
            android:background="@color/blue" />
        <Button
            android:id="@+id/client_buyhistory"
            android:layout_width="50sp"
            android:layout_height="30sp"
            android:layout_below="@+id/client_buyhistory_div"
            android:layout_marginTop="1sp"
            android:layout_toLeftOf="@+id/client_myaddress_text"
            android:background="@drawable/client_shopcar" />
        <TextView
            android:id="@+id/client_buyhistory_text"
            android:layout_width="100sp"
            android:layout_height="30sp"
            android:layout_alignRight="@+id/client_myaddress_text"
            android:layout_alignTop="@+id/client_buyhistory"
            android:text="已购买"
            android:textColor="@color/blue"
            android:textSize="24sp" />
        <View
            android:id="@+id/client_buyhistory_div_div"
            android:layout_width="fill_parent"
            android:layout_height="1sp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/client_buyhistory"
            android:background="@color/blue" />
        <TextView
            android:id="@+id/client_buyhistory_info"
            android:layout_width="160sp"
            android:layout_height="30sp"
            android:layout_alignLeft="@+id/client_buyhistory_div_div"
            android:layout_below="@+id/client_buyhistory_div_div"
            android:text="北京烤鸭100元"
            android:textColor="@color/blue" />
        <Button
            android:id="@+id/client_buyhistory_eidt"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/client_edit" />
        <View
            android:id="@+id/client_store_div"
            android:layout_width="fill_parent"
            android:layout_height="5sp"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/client_buyhistory_eidt"
            android:background="@color/blue" />
        <Button
            android:id="@+id/client_store"
            android:layout_width="50sp"
            android:layout_height="30sp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/client_store_div"
            android:background="@drawable/client_stored" />
        <TextView
            android:id="@+id/client_store_text"
            android:layout_width="80sp"
            android:layout_height="30sp"
            android:layout_alignBaseline="@+id/client_store"
            android:layout_alignBottom="@+id/client_store"
            android:layout_alignLeft="@+id/client_myaddress_text"
            android:text="收藏"
            android:textColor="@color/blue"
            android:textSize="24sp" />
        <View
            android:id="@+id/client_store_div_div"
            android:layout_width="fill_parent"
            android:layout_height="1sp"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/client_store"
            android:background="@color/blue" />
        <Button
            android:id="@+id/client_store_edit"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/client_store_info"
            android:background="@drawable/client_edit" />
        <View
            android:id="@+id/View01"
            android:layout_width="fill_parent"
            android:layout_height="5sp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:background="@color/blue" />
        <TextView
            android:id="@+id/client_store_info"
            android:layout_width="160sp"
            android:layout_height="40sp"
            android:layout_alignLeft="@+id/View01"
            android:layout_below="@+id/client_store_div_div"
            android:text="北京烤鸭30元"
            android:textColor="@color/blue" />
    </RelativeLayout>
</RelativeLayout>

但因为有个项目用到的是TabHost,对子activity的布局有限制,上面的布局超过范围。所以要用到ScrollView,为达到上面的布局,要用到<View/>控件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值