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"

   />

三、例子

   下面的界面

   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,如需转载请自行联系原作者

相关文章
|
容器
RelativeLayout(相对布局)
LinearLayout也是我们用的比较多的一个布局,我们更多的时候更钟情于他的weight(权重)属性,等比例划分,对屏幕适配还是帮助蛮大的;但是使用LinearLayout的时候也有一个问题,就是当界面比较复杂的时候,需要嵌套多层的LinearLayout,这样就会降低UI Render的效率(渲染速度),而且如果是listview或者GridView上的item,效率会更低,另外太多层LinearLayout嵌套会占用更多的系统资源,还有可能引发stackoverflow;但是如果我们使用RelativeLayout的话,可能仅仅需要一层就可以完成了,以父容器或者兄弟组件参考+margi
62 0
|
算法 Java Android开发
LinearLayout(线性布局)
本节开始讲Android中的布局,今天我们要讲解的就是第一个布局,LinearLayout(线性布局),我们屏幕适配的使用用的比较多的就是LinearLayout的weight(权重属性),在这一节里,我们会详细地解析LinearLayout,包括一些基本的属性,Weight属性的使用,以及比例如何计算,另外还会说下一个用的比较少的属性:android:divider绘制下划线!
50 0
|
XML Java Android开发
android ScrollView 吸顶效果
android ScrollView 吸顶效果
284 0
|
Android开发
Android: 解决GridView、ListView在ScrollView中出现冲突的问题
Android: 解决GridView、ListView在ScrollView中出现冲突的问题
282 0
|
XML Android开发 数据格式
Android开发中RelativeLayout相对布局
Android开发中RelativeLayout相对布局
287 0
Android开发中RelativeLayout相对布局
|
Android开发
Android零基础入门第61节:滚动视图ScrollView
原文:Android零基础入门第61节:滚动视图ScrollView    前面几期学习了ProgressBar系列组件、ViewAnimator系列组件、Picker系列组件和时间日期系列组件,接下来几期继续来学习常见的其他组件。
1709 0
|
Android开发
Android scrollview嵌套webview滑动冲突的解决方案
在Android开发中有时我们需要在scrollview中嵌套webview这时你会发现这两者的滑动事件产生了冲突导致:webview很难被滑动,即使被滑动了一点也非常不顺畅。解决方案也比较简单只需要在webview的ontouch事件中调用requestDisallowInterceptTouchEvent(true)即可。然后交给webview去处理,代码如下: mWebView.
6109 0

热门文章

最新文章