android应用开发-从设计到实现 4-7天气详情的布局
来源:互联网 发布:威海市网络推广专员 编辑:程序博客网 时间:2024/06/13 04:55
天气详情的布局
整体布局完成之后,我们开始进行天气详情
区域的布局。
这个区域由天气图标、当前温度、当日温度范围、地理位置等4个元素组成。我们可以选用Android SDK
提供的现有控件,完成布局。
这些区域的对齐分配,我们可以借助嵌套更多的LinearLayout
来完成,
ImageView
:Android SDK
提供的专门显示图片的控件; TextView
:Android SDK
提供的专门显示文字的控件;
上下区域的划分
将天气图标、当前温度、当日温度范围归为上半区域,地理位置归为下半区域,
- 使用
LinearLayout
进行分割; - 给
天气详情
区域设置Primary Color
-#3F51B5
作为背景颜色; - 上半区域设置高度
android:layout_height
为112dp
,上边距android:layout_marginTop
为24dp
; - 下半区域使用所有剩下的空间;
<LinearLayout android:layout_width="match_parent" android:layout_height="240dp" android:background="#3F51B5" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="112dp" android:layout_marginTop="24dp" android:background="#FFFF0000"> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FF00FF00"> </LinearLayout></LinearLayout>
天气图标与当日温度
天气图标与当日温度左右并列,各自占据96dp\*144dp
的区域。两个区域之间间隔24dp
,
- 为
LinearLayout
设置horizontal
属性,使其内部的布局或控件水平排列,把它的高度修改成wrap_content
; 将
ImageView
放置在左边,宽度设置成144dp
,高度设置成match_parent
;同时给
ImageView
设定一个id
-android:id="@+id/weather_icon"
。设定了
id
,以后再代码中获取它对应的控件就很方便了,只需要像这样,ImageView im = (ImageView) findViewById(R.id.weather_icon);
java
源码就能通过R.id.weather_icon
,将布局文件中的ImageView
找了出来,转换成了可以通过java
代码操作的对象。将另一个
LinearLayout
放置在右边,宽度设置成144dp
,高度设置成match_parent
;
<LinearLayout android:layout_width="match_parent" android:layout_height="112dp" android:layout_marginTop="24dp" android:background="#FFFF0000" android:orientation="horizontal"> <ImageView android:id="@+id/weather_icon" android:layout_width="144dp" android:layout_height="match_parent" android:background="#FFFFFF00"/> <LinearLayout android:layout_width="144dp" android:layout_height="match_parent" android:background="#FF0000FF"> </LinearLayout></LinearLayout>
此时布局很紧促,并且没有居中对齐,需要做进一步的设置,
- 为
LinearLayout
设置android:gravity="center"
属性;让它内部的组件都能够居中放置; - 为了将
ImageView
与LinearLayout
分开,在它们水平位置之间设置一个24dp
的边距:给温度区域
设置上24dp
的左边距android:layout_marginLeft
;
<LinearLayout android:layout_width="match_parent" android:layout_height="112dp" android:layout_marginTop="24dp" android:background="#FFFF0000" android:orientation="horizontal" android:gravity="center"> <ImageView android:id="@+id/weather_icon" android:layout_width="144dp" android:layout_height="match_parent" android:background="#FFFFFF00"/> <LinearLayout android:layout_width="144dp" android:layout_height="match_parent" android:background="#FF0000FF" android:layout_marginLeft="24dp"> </LinearLayout></LinearLayout>
layout_gravity与gravity
前面提到了gravity
。无论控件还是布局都可以设置它本身相对于父布局的偏好位置,例如是居中还是靠左。例如一个放到LinearLayout
中的Button
,它希望自己位于父布局内部靠上的位置,就可以添加android:layout_gravity
属性,设置值为top
,
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_gravity="top" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!"/></LinearLayout>
其它可以设置的值还有bottom
left
right
center
center_horizontal
center_vertical
。
控件或者布局,也可以设置自己的内容相对于内部的位置。例如上面的例子中,要将Button
放到LinearLayout
底部靠右的位置,可以为其android:gravity
属性设置bottom|right
。这里的|
表示并且
,
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="bottom|right"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello world!"/></LinearLayout>
添加图片
将天气图标设置给ImageView
,
- 添加
android:src
属性,把图标id-ic_sunny_cloudy_l
设置给它; - 添加
android:scaleType
属性,设置值为center
;
<LinearLayout android:layout_width="match_parent" android:layout_height="112dp" android:layout_marginTop="24dp" android:orientation="horizontal" android:gravity="center"> <ImageView android:id="@+id/weather_icon" android:layout_width="144dp" android:layout_height="match_parent" android:src="@mipmap/ic_sunny_cloudy_l" android:scaleType="center"/> ......</LinearLayout>
添加温度
使用LinearLayout
将温度显示分为上下两个部分,显示温度的控件使用TextView
- 给
LinearLayout
的orientation
设置为vertical
; - 为
LinearLayout
设置android:gravity="center_horizontal"
属性;让它内部的组件都能够居中放置; - 修改
LinearLayout
的高度为match_parent
; - 放入2个
TextView
,用来分别当日温度和温度范围,并分别给它们指定id
-current_temperature
和temperature_range
;
<LinearLayout android:layout_width="match_parent" android:layout_height="112dp" android:layout_marginTop="24dp" android:orientation="horizontal" android:gravity="center"> ...... <LinearLayout android:layout_width="144dp" android:layout_height="match_parent" android:layout_marginLeft="24dp" android:orientation="vertical" android:gravity="center_horizontal"> <TextView android:id="@+id/current_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <TextView android:id="@+id/temperature_range" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout></LinearLayout>
设置当前温度
给当前温度设置上一个预设的显示内容:
- 设置
TextView
显示的文字内容,android:text="23°"
; - 设置
TextView
文字的大小,android:textSize="56sp"
; - 设置
TextView
文字的颜色,android:textColor="#b3ffffff"
;
<TextView android:id="@+id/current_temperature" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="23°" android:textColor="#b3ffffff" android:textSize="56sp"/>
设置温度范围
给温度范围设置上一个预设的显示内容:
- 设置
TextView
显示的文字内容,android:text="17℃~25℃"
; - 设置
TextView
文字的大小,android:textSize="24sp"
; - 设置
TextView
文字的颜色,android:textColor="#ffffffff"
;
<TextView android:id="@+id/temperature_range" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="17℃~25℃" android:textColor="#ffffffff" android:textSize="24sp"/>
添加位置信息
将TextView
添加到下部的LinearLayout
当中:
- 位置为垂直居中,给
LinearLayout
设置android:gravity="center_vertical"
; - 设置
TextView
的左边距为24dp
; - 设置
TextView
显示的文字内容,android:text="成都
; - 设置
TextView
文字的大小,android:textSize="34sp"
; - 设置
TextView
文字的颜色,android:textColor="#ffffffff"
; TextView
的id
设置成location
<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical"> <TextView android:id="@+id/weather_location" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="24dp" android:text="成都" android:textColor="#ffffffff" android:textSize="34sp"/></LinearLayout>
这个区域的界面设计只是在第1阶段
的实现中会使用到。当进入到第2阶段
-使用Material
设计规范来实现的时候,是不需要这个布局的,因为安卓系统能自动实现这个信息的显示。
现在,天气详情的布局就全部完成了。
/*******************************************************************/
* 版权声明
* 本教程只在CSDN和安豆网发布,其他网站出现本教程均属侵权。
*另外,我们还推出了Arduino智能硬件相关的教程,您可以在我们的网店跟我学Arduino编程中购买相关硬件。同时也感谢大家对我们这些码农的支持。
*最后再次感谢各位读者对安豆
的支持,谢谢:)
/*******************************************************************/
- android应用开发-从设计到实现 4-7天气详情的布局
- android应用开发-从设计到实现 4-9 天气指数列表的布局
- android应用开发-从设计到实现 3-7 静态原型的更多天气信息
- android应用开发-从设计到实现 4-6界面的整体布局
- android应用开发-从设计到实现 4-8 天气预报的布局
- android应用开发-从设计到实现 2-2 界面布局
- android应用开发-从设计到实现 4-3应用的创建与部署
- android应用开发-从设计到实现 2-7 声音与触觉的利用
- android应用开发-从设计到实现 2-4 文字的使用
- android应用开发-从设计到实现 3-4 静态原型的状态栏
- android应用开发-从设计到实现 4-5资源的导入
- android应用开发-从设计到实现 1-1 创意
- android应用开发-从设计到实现 4-1 开发产品
- android应用开发-从设计到实现 4-2 开发环境搭建
- android应用开发-从设计到实现 2-3 颜色的运用
- android应用开发-从设计到实现 2-3 颜色的运用(一)
- android应用开发-从设计到实现 2-5 图标的使用
- android应用开发-从设计到实现 3-8 静态原型的Appbar折叠
- CSS3 选择器浏览器兼容性汇总 IE8
- MySQL LOAD DATA INFILE 导入 CSV 格式数据
- leetcode(56). Merge Intervals
- 音乐类App原型制作分享-Qello Concerts
- neural networks deep learning Python Basics with numpy (optional) Homework
- android应用开发-从设计到实现 4-7天气详情的布局
- Divide Two Integers LeetCode 解题报告
- 好用的工具
- Alpha选股:资本资产定价模型(CAPM)
- poj 3695 Rectangles(容斥原理)
- 题目1040:Prime Number
- 源代码安全管理
- 【jzoj5329】【NOIP2017提高A组模拟8.22】【时间机器】【数据结构】【扫描线】
- SAP/ABAP: Smartforms, SSF_OPEN, SSF_CLOSE