拓展训练 1- 界面布局 1. 界面布局的重要性做应用程序, 界面是最基本的 Andorid 的界面, 需要写在 res/layout 的 xml 里面, 一般情况下一个 xml 对应一个界面 Android 界面布局有点像写 html( 连注释代码的方式都一样 ), 要先给 Android 定框架, 然后再在框架里面放控件,Android 提供了几种框架,AbsoluteLayout,LinearLayout, RelativeLayout,TableLayout,FrameLayout FrameLayout: 里面只可以有一个控件, 并且不能设计这个控件的位置, 控件会放到左上角 ; LinearLayout: 里面可以放多个控件, 但是一行只能放一个控件 ; TableLayout: 这个要和 TableRow 配合使用, 很像 html 里面的 table; AbsoluteLayout: 里面可以放多个控件, 并且可以自己定义控件的 x,y 的位置 ; RelativeLayout: 里面可以放多个控件, 不过控件的位置都是相对位置 2. 拓展实例电大应用程序的界面布局采用的是 LinearLayout 和 FrameLayout 相结合的方式 如图 1 所示, 是该应用程序结构图 : 图 1 程序结构图 2.1 界面布局 xml 文件
res/layout 目录下的 main.xml 源码 : <?xml version="1.0" encoding="utf 8"?> <TabHost android:id="@android:id/tabhost" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <LinearLayout android:orientation="vertical" android:layout_height="fill_parent"> <FrameLayout android:id="@+id/msg_title" android:visibility="gone" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_wb" android:layout_height="wrap_content" android:background="@drawable/titlebar_lightgray_bg"></textview> </FrameLayout> <FrameLayout android:id="@android:id/tabcontent" android:layout_height="0.0dip" android:layout_weight="1.0" android:theme="@android:style/theme.black.notitlebar.fullscreen"/> <TabWidget android:id="@android:id/tabs" android:visibility="gone" android:layout_height="wrap_content" android:layout_weight="0.0" /> <RadioGroup android:gravity="center_vertical" android:layout_gravity="bottom" android:orientation="horizontal" android:id="@id/main_radio" android:background="@drawable/header" android:layout_height="wrap_content"> <RadioButton android:id="@+id/radio_button0" android:tag="radio_button0"
android:text="@string/main_home" android:drawabletop="@drawable/home" <RadioButton android:id="@+id/radio_button7" android:tag="radio_button7" android:text="@string/ddjj" android:drawabletop="@drawable/ddjj" <RadioButton android:id="@+id/radio_button1" android:tag="radio_button1" android:text="@string/main_news" android:drawabletop="@drawable/diqiu" <RadioButton android:id="@+id/radio_button2" android:tag="radio_button2" android:text="@string/main_my_info" android:drawabletop="@drawable/shu" <RadioButton android:id="@+id/radio_button3" android:tag="radio_button3" android:text="@string/mobile_classroom" android:drawabletop="@drawable/mobile_classroom" <RadioButton android:id="@+id/radio_button5" android:tag="radio_button5"
android:text="@string/video" android:drawabletop="@drawable/video" <RadioButton android:id="@+id/radio_button6" android:tag="radio_button6" android:text="@string/media" android:drawabletop="@drawable/media" <RadioButton android:id="@+id/radio_button8" android:tag="radio_button8" android:text="@string/more" android:drawabletop="@drawable/more" style="@style/main_tab_bottom"/> </RadioGroup> </LinearLayout> </TabHost> 2.2 界面程序源码 src 目录下的 shouye.java 源码 : package my.dianda.third; import android.app.tabactivity; import android.content.intent; import android.os.bundle; //import android.util.log; import android.widget.radiogroup; import android.widget.radiogroup.oncheckedchangelistener; import android.widget.tabhost; import android.widget.tabhost.tabspec; //import pei.popupwindow.popupwindow_peiactivity;
public class shouye extends TabActivity { /** Called when the activity is first created. */ public TabHost mth; public static final String TAB_HOME=" 首页 "; public static final String TAB_NEWS=" 电大新闻 "; public static final String TAB_Library=" 移动图书馆 "; public static final String TAB_CLASS=" 移动课堂 "; // public static final String TAB_ZHAO=" 语音服务 "; public static final String TAB_Media=" 音频服务 "; public static final String TAB_Vedio=" 视频服务 "; public static final String TAB_JIAN=" 电大简介 "; public static final String TAB_MORE="More"; public RadioGroup radiogroup; @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); mth=this.gettabhost(); TabSpec ts1=mth.newtabspec(tab_home).setindicator(tab_home); ts1.setcontent(new Intent(shouye.this,zhuye.class)); mth.addtab(ts1); TabSpec ts2=mth.newtabspec(tab_news).setindicator(tab_news); ts2.setcontent(new Intent(shouye.this,news.class)); mth.addtab(ts2); TabSpec ts3=mth.newtabspec(tab_library).setindicator(tab_library); ts3.setcontent(new Intent(shouye.this,book.class)); mth.addtab(ts3); TabSpec ts4=mth.newtabspec(tab_class).setindicator(tab_class); ts4.setcontent(new Intent(shouye.this,yidongke.class)); mth.addtab(ts4); // TabSpec ts5=mth.newtabspec(tab_zhao).setindicator(tab_zhao); // ts5.setcontent(new Intent(shouye.this,yuyin.class)); // mth.addtab(ts5); TabSpec ts6=mth.newtabspec(tab_vedio).setindicator(tab_vedio);
ts6.setcontent(new Intent(shouye.this,vediohome.class)); mth.addtab(ts6); TabSpec ts7=mth.newtabspec(tab_media).setindicator(tab_media); ts7.setcontent(new Intent(shouye.this,mediahome.class)); mth.addtab(ts7); TabSpec ts8=mth.newtabspec(tab_jian).setindicator(tab_jian); ts8.setcontent(new Intent(shouye.this,net.fiex.list.FlexListActivity.class)); mth.addtab(ts8); TabSpec ts9=mth.newtabspec(tab_more).setindicator(tab_more); ts9.setcontent(new Intent(shouye.this,pei.popupwindow.PopUpWindow_PeiActivity.class)); mth.addtab(ts9); this.radiogroup=(radiogroup)findviewbyid(r.id.main_radio); radiogroup.setoncheckedchangelistener(new OnCheckedChangeListener() { { @Override public void oncheckedchanged(radiogroup group, int checkedid) // TODO Auto generated method stub switch(checkedid){ case R.id.radio_button0: mth.setcurrenttabbytag(tab_home); case R.id.radio_button7: mth.setcurrenttabbytag(tab_jian); case R.id.radio_button1: mth.setcurrenttabbytag(tab_news); case R.id.radio_button2: mth.setcurrenttabbytag(tab_library); case R.id.radio_button3: mth.setcurrenttabbytag(tab_class); // case R.id.radio_button4: // mth.setcurrenttabbytag(tab_zhao);
// case R.id.radio_button5: mth.setcurrenttabbytag(tab_vedio); case R.id.radio_button6: mth.setcurrenttabbytag(tab_media); }); } case R.id.radio_button8: mth.setcurrenttabbytag(tab_more); } } 2.3 界面布局效果图 程序运行后, 显示主界面结果如图 2 所示 : 3. 源程序下载 Layout.zip 图 2 程序运行后的界面布局