ScrollView不能用RelativeLayout的解决方案

  1. 云栖社区>
  2. 博客>
  3. 正文

ScrollView不能用RelativeLayout的解决方案

科技小先锋 2017-11-07 02:29:00 浏览782
展开阅读全文

RelativeLayout的意义:

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

   

一、ScrollView局限:

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

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


二、View用法

   <View

   android:layout_width="fill_parent"
       android:layout_height="10sp"

   />

三、例子

   下面的界面

   092719843.jpg

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

   RelativeLayout代码

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<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/>控件。

2)使用view控件的代码如下

   

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fadingEdge="vertical"
    android:scrollbars="vertical" >
                                                                                  
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
        <TextView
        android:layout_width="match_parent"
        android:layout_height="30sp"
        android:background="@color/black"
        android:singleLine="true"
        android:text="用户名"
        android:textColor="@color/white"
        android:textSize="24sp" />
         <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
             <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" />
              <Button
        android:id="@+id/client_tel_edit"
        android:layout_width="50sp"
        android:layout_height="30sp"
        android:background="@drawable/cliebt_button_edittel"
        android:text="编辑"
        android:textColor="@color/blue" />
         </LinearLayout>>
                                                                                    
    <TextView
        android:id="@+id/client_tel"
        android:layout_width="match_parent"
        android:layout_height="30sp"
        android:background="@color/black"
        android:ems="12"
        android:singleLine="true"
        android:text="电话号码"
        android:textColor="@color/white" />
                                                                                 
    <View
        android:layout_width="fill_parent"
        android:layout_height="10sp"
        android:background="@color/blue" />
     <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_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:text="我的地址"
            android:textColor="@color/blue"
            android:textSize="24sp" />
     </LinearLayout>
   <View
            android:layout_width="fill_parent"
            android:layout_height="1sp"
            android:background="@color/blue" />
                                                                                    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
      <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" />
        <View
        android:layout_width="100sp"
        android:layout_height="40sp"
        />
                                                                                    
        <Button
            android:id="@+id/client_myaddress_edit"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:background="@drawable/client_edit" />
        </LinearLayout>
        <View
        android:layout_width="fill_parent"
        android:layout_height="10sp"
        android:background="@color/blue" />
          <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
        <Button
            android:id="@+id/client_buyhistory"
            android:layout_width="50sp"
            android:layout_height="30sp"
            android:layout_marginTop="1sp"
            android:background="@drawable/client_shopcar" />
        <TextView
            android:id="@+id/client_buyhistory_text"
            android:layout_width="100sp"
            android:layout_height="30sp"
            android:text="已购买"
            android:textColor="@color/blue"
            android:textSize="24sp" />
        </LinearLayout>
         <View
        android:id="@+id/view1"
        android:layout_width="fill_parent"
        android:layout_height="1sp"
        android:background="@color/blue" />
          <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
         <TextView
            android:id="@+id/client_buyhistory_info"
            android:layout_width="160sp"
            android:layout_height="30sp"
            android:text="北京烤鸭100元"
            android:textColor="@color/blue" />
         <View
        android:layout_width="100sp"
        android:layout_height="40sp"
        />
                                                                                       
        <Button
            android:id="@+id/client_buyhistory_eidt"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:background="@drawable/client_edit" />
          </LinearLayout>
       <View
        android:layout_width="fill_parent"
        android:layout_height="1sp"
        android:background="@color/blue" />
                                                                                       
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
        <Button
            android:id="@+id/client_store"
            android:layout_width="50sp"
            android:layout_height="30sp"
            android:background="@drawable/client_stored" />
        <TextView
            android:id="@+id/client_store_text"
            android:layout_width="80sp"
            android:layout_height="30sp"
            android:text="收藏"
            android:textColor="@color/blue"
            android:textSize="24sp" />
        <View
            android:layout_width="fill_parent"
            android:layout_height="1sp"
            android:background="@color/blue" />
                                                                                      
    </LinearLayout>    
       <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@drawable/account_pic_back"
        >
    <TextView
            android:id="@+id/client_store_info"
            android:layout_width="160sp"
            android:layout_height="40sp"
            android:text="北京烤鸭30元"
            android:textColor="@color/blue" />
    <View
        android:layout_width="100sp"
        android:layout_height="40sp"
        />
    <Button
            android:id="@+id/client_store_edit"
            android:layout_width="40sp"
            android:layout_height="40sp"
            android:background="@drawable/client_edit" />
    </LinearLayout
    <View
            android:layout_width="fill_parent"
            android:layout_height="5sp"
            android:background="@color/blue" />
    </LinearLayout>
                                                                                  
</ScrollView>



通过View完美的调出RelativeLayout的效果


本文转自lilin9105 51CTO博客,原文链接:http://blog.51cto.com/7071976/1217017,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
科技小先锋
+ 关注