学习了快一学期的Android了,之前的知识点都是零散的学习的,只有当我们真正的去把他们用起来的时候才会发现难点,自己才会独立尝试去解决某个问题。接下来是我的一个简单的多功能时钟的小实战(视频资源https://pan.baidu.com/s/1mL8VUVC-9W9lO6IEpobDZg 密码:2ajv)
具体的效果可以参考手机上的时钟。
首先我们来看一看布局文件layout_main.xml
我们总的来看一下整个布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.clock.TimeView
android:id="@+id/tabTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</com.example.clock.TimeView>
<com.example.clock.AlarmView
android:id="@+id/tabAlarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<span style="white-space:pre"> </span>……
</com.example.clock.AlarmView>
<com.example.clock.TimerView
android:id="@+id/tabTimer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<span style="white-space:pre"> </span>……
</com.example.clock.TimerView>
<com.example.clock.StopWatchView
android:id="@+id/tabStopWatch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<span style="white-space:pre"> </span>……
</com.example.clock.StopWatchView>
</FrameLayout>
</LinearLayout>
</TabHost>
</FrameLayout>
整个布局整的是一个FrameLayout,我们在里面放了一个TabHost,接下来我们就可以在里面直接添加自己想要的布局了,可能初学者初一看会有那么一个疑问,就是<com.example.clock.……></com.example.clock.……>这个是什么东西??这是一个自定义的控件,我们创建的一个继承了LinearLayout的一个类(讲解可以参考这里http://blog.csdn.net/guozh/article/details/7662374),上面我们看到了四个这样的标签,表示我们有四个这样的Tab页面。关于布局的东西这里就不多讲了,之后会把我自己在学习过程中的一些不懂,以及相关的知识点上传到资源中,大家可以下载来看看。
这是完整的布局文件代码
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.clock.TimeView
android:id="@+id/tabTime"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tvTime"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
</com.example.clock.TimeView>
<com.example.clock.AlarmView
android:id="@+id/tabAlarm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lvListAlarm"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<Button
android:id="@+id/btnAddAlarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_alarm" >
</Button>
</com.example.clock.AlarmView>
<com.example.clock.TimerView
android:id="@+id/tabTimer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
<EditText
android:id="@+id/etHour"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=":"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/etMin"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="number"
android:singleLine="true"