接上一节,下一步,进入主桌面。感觉做android客户端,最主要的还是要美工好啊,呵呵,我个人美工很差劲,也就这二流子进不去也称不上三流子的,二不二三不三的水平。
主界面根节点是LinearLayout,线性布局,如果不懂布局的朋友,可以找找这方面的资料,由于最近项目很紧张,没时间做自己的事,以后闲下来,好好整理一份文档。
废话不说了,看代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/software_empty_background" android:orientation="vertical" > <TextView android:id="@+id/textTitle" style="@style/TitleBarStyle" android:text="@string/app_name" /> <LinearLayout android:id="@+id/buttonForNews" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_for_maindesktop_item" android:orientation="horizontal" > <ImageButton android:layout_width="50dip" android:layout_height="50dip" android:layout_margin="10dip" android:background="@drawable/selector_for_news_button" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textViewForNews" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_news" android:textColor="@color/DarkGrayColor" android:textSize="@dimen/NewsTitleFontSize" /> <TextView android:id="@+id/textViewForNewsDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_news_description" android:textColor="@color/SmallDarkGrayColor" android:textSize="@dimen/NewsDescriptionFontSize" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/buttonForSoftware" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_for_maindesktop_item" android:orientation="horizontal" > <ImageButton android:layout_width="50dip" android:layout_height="50dip" android:layout_margin="10dip" android:background="@drawable/selector_for_software_button"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textViewForSoftware" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_software" android:textColor="@color/DarkGrayColor" android:textSize="@dimen/NewsTitleFontSize" /> <TextView android:id="@+id/textViewForSoftwareDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_software_description" android:textColor="@color/SmallDarkGrayColor" android:textSize="@dimen/NewsDescriptionFontSize" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/buttonForTencent" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_for_maindesktop_item" android:orientation="horizontal" > <ImageButton android:layout_width="50dip" android:layout_height="50dip" android:layout_margin="10dip" android:background="@drawable/selector_for_tencent_button"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textViewForTencent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_tencent" android:textColor="@color/DarkGrayColor" android:textSize="@dimen/NewsTitleFontSize" /> <TextView android:id="@+id/textViewForTencentDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_tencent_description" android:textColor="@color/SmallDarkGrayColor" android:textSize="@dimen/NewsDescriptionFontSize" /> </LinearLayout> </LinearLayout> <LinearLayout android:id="@+id/buttonForAboutUs" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/selector_for_maindesktop_item" android:orientation="horizontal" > <ImageButton android:layout_width="50dip" android:layout_height="50dip" android:layout_margin="10dip" android:background="@drawable/selector_for_aboutus_button"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="left|center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textViewForAboutUs" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_aboutus" android:textColor="@color/DarkGrayColor" android:textSize="@dimen/NewsTitleFontSize" /> <TextView android:id="@+id/textViewForAboutUsDescription" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/MaindesktopText_for_aboutus_description" android:textColor="@color/SmallDarkGrayColor" android:textSize="@dimen/NewsDescriptionFontSize" /> </LinearLayout> </LinearLayout> </LinearLayout>
大概是这样的,最外面一个LinearLayout包围,第一行是显示标题的TextView,下面的每一行都是一个单独的LinearLayout,实现LinearLayout按下变背景色的是这句:
android:background="@drawable/selector_for_maindesktop_item"
看代码:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/line_item_background_pressed" android:state_pressed="true" /> <item android:drawable="@drawable/line_item_background" /> </selector>
一个selector指定了当前背景色变化的状态,android:state_pressed="true"表示,按下的时候是什么状态,<item android:drawable="@drawable/line_item_background" />这一句表示,一般情况下的状态,至于drawable/line_item_background和drawable/line_item_background_pressed,是两张图而已,截图看看哈:
背景貌似是平铺的。
说着说着,真不知道从哪里说起了,说点后台代码吧。
package com.mrhuo.mrhuowebclient; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.LinearLayout; import com.mrhuo.mrhuowebclient.properties.UserInformation; public class MainDesktopActivity extends Activity { private String postStr =null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maindesktop); ShowDefault(); } private void ShowDefault() { ((LinearLayout)this.findViewById(R.id.buttonForNews)).setOnClickListener(onclickListener); ((LinearLayout)this.findViewById(R.id.buttonForSoftware)).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Core.ShowInfo(MainDesktopActivity.this, "提示", "产品模块正在开发中,不定时开放!欢迎期待!\r\n\thttp://www.mrhuo.com"); } }); ((LinearLayout)this.findViewById(R.id.buttonForTencent)).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://t.qq.com/HGates"))); } }); ((LinearLayout)this.findViewById(R.id.buttonForAboutUs)).setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Core.ShowInfo(MainDesktopActivity.this, R.string.MenuText_AboutUs, R.string.AboutSystem); } }); } /** * 菜单被按下执行的事件 */ public boolean onOptionsItemSelected(MenuItem item) { super.onOptionsItemSelected(item); switch (item.getItemId()) { case ConstValue.MENU_VISIT: startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.mrhuo.com"))); break; case ConstValue.MENU_SENDINFO: Intent intent=new Intent(MainDesktopActivity.this,SendInformationActivity.class); startActivity(intent); break; case ConstValue.MENU_ABOUT_PROGRAM: Core.ShowInfo(MainDesktopActivity.this, R.string.MenuText_AboutProgram, R.string.AboutProgram); break; case ConstValue.MENU_EXIT: CreateExitProgramDialog(); break; } return true; } /** * 创建功能菜单 */ @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); menu.add(Menu.CATEGORY_SYSTEM, ConstValue.MENU_VISIT, 0, R.string.MenuText_Visit); menu.add(Menu.CATEGORY_SYSTEM, ConstValue.MENU_SENDINFO,1, R.string.MenuText_SendInfo); menu.add(Menu.CATEGORY_SYSTEM, ConstValue.MENU_ABOUT_PROGRAM, 2, R.string.MenuText_AboutProgram); return true; } View.OnClickListener onclickListener=new View.OnClickListener(){ public void onClick(View view) { Intent intent=new Intent(MainDesktopActivity.this,NewsActivity.class); startActivity(intent); } }; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { boolean flag=false; if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { CreateExitProgramDialog(); flag=true; } return flag; } /** * 显示退出程序确认框 */ private void CreateExitProgramDialog() { new AlertDialog.Builder(MainDesktopActivity.this) .setIcon(android.R.drawable.ic_dialog_info) .setTitle("系统提示") .setMessage("确认要退出系统吗?") .setPositiveButton("确认", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); System.exit(1); android.os.Process.killProcess(android.os.Process.myPid()); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }) .create().show(); } }
也没有多大的难点,主要个人觉得,难点在于线程同步问题,用好Handler和Runnable,有关这方面的文章,本人会在最近专门写一篇文章讨论。
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(http://www.mrhuo.com)));
表示,在浏览器里打开http://www.mrhuo.com网址、
Core.ShowInfo是自己写了一个包,封装了一下,AlertDialog,代码如下:
/** * 用于显示具有确认按钮的提示对话框 * @param context * @param title * @param message */ public static void ShowInfo(MainDesktopActivity context, int title, int message) { AlertDialog.Builder builder = new Builder(context); builder.setMessage(message); builder.setIcon(android.R.drawable.ic_dialog_info); builder.setTitle(title); builder.setPositiveButton("确认", new OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); }
主界面里面的内容也就这些,没什么功能上的难度,相信大家可以自己解决的
期待下一节,新闻列表的展示,我的服务端是。net写的,Json传送数据