最近在自学android,先练手做一个小项目BMI。顺便了解一下如何写博客,如何使用GitHub,第一次写博客,有不好的地方还请大神赐教。
BMI指数(身体质量指数,简称体质指数,又称体重指数,英文为Body Mass Index,简称BMI)是用体重公斤数除以身高米数平方得出的数字。
BMI指数计算
体质指数(BMI)=体重(kg)÷身高^2(m)
BMI值 | 身体状况 |
---|---|
bmi<18.5 | 过轻 |
18.5<=bmi<24 | 健康体重 |
24<=bmi<28 | 超重 |
bmi>=28 | 肥胖 |
好了,了解了BMI之后开始正式写代码。
其实做这个项目大体就需要两步,第一步是编写界面,第二步是按钮的点击事件。
首先先建一个BMI的android项目。
先来几张app截图
现在开始我们的项目之旅~
编写界面
首先先要写这个界面,我们可以采用线性布局 ,垂直分布,其中身高和体重那里又需要一个线性布局,要把三个控件水平排放,好,直接上代码。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mjy_bmi.MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dp"
android:text="@string/title"
android:textColor="#ff00ff"
android:textSize="30dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_gravity="center"
android:layout_weight="1"