安卓市场---框架搭建4

来源:互联网 发布:双11抢购软件 编辑:程序博客网 时间:2024/05/03 12:58

书接上回,在上面一片博客中,我们添加了TabHost,我们接着进行我们的框架搭建。

在这里,我们首先完成需要的那几个类,包括 “首页”, “分类”, “排行”, “推荐”, “主题”,仅仅就是搭建一个小型的框架,其中,稍微麻烦一点的就是首页的界面。我们先来看下。

根据我们上文中的关于首页的图片,我们知道,在我们的首页中,有上中下三个,下面的TabHost已经有了,我们来定义一下上面的框框。

首先,在我们的res/layout文件夹中新增一个xml文件,命名为:activity_home.xml,并选择RelativeLayout布局。

我们需要编写一个RelativeLayout节点,该节点中有一个头像ImageView,一个文本框TextView,一个搜索ImageView还有一个二维码ImageView。

我们来布局一下:

    <RelativeLayout        android:id="@+id/rl_head"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/mbarcolor" >        <RelativeLayout            android:id="@+id/rl_in_head"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            android:layout_margin="5dip" >            <TextView                android:id="@+id/tv_contact"                android:layout_width="40dip"                android:layout_height="40dp"                android:background="@drawable/contact_press"                android:clickable="true" />            <RelativeLayout                android:id="@+id/rl_search"                android:layout_width="match_parent"                android:layout_height="40dip"                android:layout_marginLeft="8dp"                android:layout_toRightOf="@id/tv_contact"                android:background="@drawable/shape_rectangle" >                <ImageView                    android:id="@+id/iv_search"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_centerVertical="true"                    android:background="@drawable/search" />                <TextView                    android:id="@+id/tv_search"                    android:layout_width="wrap_content"                    android:layout_height="30dp"                    android:layout_centerVertical="true"                    android:layout_marginLeft="5dp"                    android:layout_toRightOf="@id/iv_search"                    android:gravity="center_vertical"                    android:text="@string/tv_search_text"                    android:textColor="@color/lightgrey"                    android:textSize="20sp" />                <ImageView                    android:id="@+id/iv_dia"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentRight="true"                    android:layout_marginRight="5dp"                    android:background="@drawable/diecode" />            </RelativeLayout>        </RelativeLayout>        <RelativeLayout            android:id="@+id/rl_blank"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/rl_in_head" >        </RelativeLayout>    </RelativeLayout>

接下来是一个广告栏,是一个循环播放的Gallery,同时在Gallery下面还有一些点,用于显示哪一个图像正在被展示,这些点可以放在LinearLayout中,我们来看一下定义。

    <RelativeLayout        android:id="@+id/rl_advers"        android:layout_width="match_parent"        android:layout_height="100dp"        android:layout_below="@id/rl_head" >        <com.sdu.ui.AdGallery            android:id="@+id/app_advers"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <LinearLayout            android:id="@+id/ovalLayout"            android:layout_width="match_parent"            android:layout_height="10dip"            android:layout_below="@+id/app_advers"            android:background="#FFFFFF"            android:gravity="center"            android:orientation="horizontal" >        </LinearLayout>    </RelativeLayout>

再往下其实应该就是展示APP的ListView了,在这里我们先不写,后面添加内容的时候再添加。

我们先看一下整体的布局:

    <RelativeLayout 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:background="@color/mgrey" >    <RelativeLayout        android:id="@+id/rl_head"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="@color/mbarcolor" >        <RelativeLayout            android:id="@+id/rl_in_head"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentTop="true"            android:layout_margin="5dip" >            <TextView                android:id="@+id/tv_contact"                android:layout_width="40dip"                android:layout_height="40dp"                android:background="@drawable/contact_press"                android:clickable="true" />            <RelativeLayout                android:id="@+id/rl_search"                android:layout_width="match_parent"                android:layout_height="40dip"                android:layout_marginLeft="8dp"                android:layout_toRightOf="@id/tv_contact"                android:background="@drawable/shape_rectangle" >                <ImageView                    android:id="@+id/iv_search"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_centerVertical="true"                    android:background="@drawable/search" />                <TextView                    android:id="@+id/tv_search"                    android:layout_width="wrap_content"                    android:layout_height="30dp"                    android:layout_centerVertical="true"                    android:layout_marginLeft="5dp"                    android:layout_toRightOf="@id/iv_search"                    android:gravity="center_vertical"                    android:text="@string/tv_search_text"                    android:textColor="@color/lightgrey"                    android:textSize="20sp" />                <ImageView                    android:id="@+id/iv_dia"                    android:layout_width="30dp"                    android:layout_height="30dp"                    android:layout_alignParentRight="true"                    android:layout_marginRight="5dp"                    android:background="@drawable/diecode" />            </RelativeLayout>        </RelativeLayout>        <RelativeLayout            android:id="@+id/rl_blank"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_below="@id/rl_in_head" >        </RelativeLayout>    </RelativeLayout>    <RelativeLayout        android:id="@+id/rl_advers"        android:layout_width="match_parent"        android:layout_height="100dp"        android:layout_below="@id/rl_head" >        <com.sdu.ui.AdGallery            android:id="@+id/app_advers"            android:layout_width="match_parent"            android:layout_height="wrap_content" />        <LinearLayout            android:id="@+id/ovalLayout"            android:layout_width="match_parent"            android:layout_height="10dip"            android:layout_below="@+id/app_advers"            android:background="#FFFFFF"            android:gravity="center"            android:orientation="horizontal" >        </LinearLayout>    </RelativeLayout></RelativeLayout>

好了,布局完了界面,我们在src/com.sdu.activities文件夹中新建一个activity,该activity命名为HomeActivity并继承自之前我们定义好的BaseActivity,新建完成之后,其中自动生成的initWidget()函数里面,添加两句话:

首先,我们需要的是全屏的,所以需要添加这个

    requestWindowFeature(Window.FEATURE_NO_TITLE);

同时,我们的activity需要绑定刚刚我们定义的布局界面:

    setContentView(R.layout.activity_home);

下面附上我的代码:

    package com.sdu.activities;    import net.tsz.afinal.FinalBitmap;    import com.sdu.androidmarket.R;    import com.sdu.ui.AdGallery;    import com.sdu.utils.AppLog;    import android.content.Intent;    import android.view.View;    import android.widget.AdapterView;    import android.widget.ImageView;    import android.widget.LinearLayout;    import android.widget.TextView;    import android.view.Window;    public class HomeActivity extends BaseActivity {        private TextView tv_search;        private TextView tv_contact;        private ImageView iv_dia;        private AdGallery app_advers;        private LinearLayout ovalLayout; // 圆点容器        /** 图片id的数组,本地测试用 */        private int[] imageId = new int[] { R.drawable.test, R.drawable.test,                R.drawable.test, R.drawable.test };        /** 图片网络路径数组 */        private String[] mris = {                "http://img.my.csdn.net/uploads/201312/14/1386989803_3335.PNG",                "http://img.my.csdn.net/uploads/201312/14/1386989613_6900.jpg",                "http://img.my.csdn.net/uploads/201312/14/1386989802_7236.PNG" };        @Override        public void initWidget() {            requestWindowFeature(Window.FEATURE_NO_TITLE);            setContentView(R.layout.activity_home);            FinalBitmap.create(this); // android 框架 这里用于加载网络图片             tv_search = (TextView)findViewById(R.id.tv_search);            tv_contact = (TextView)findViewById(R.id.tv_contact);            iv_dia = (ImageView)findViewById(R.id.iv_dia);            tv_search.setOnClickListener(this);            tv_contact.setOnClickListener(this);            iv_dia.setOnClickListener(this);            app_advers = (AdGallery)findViewById(R.id.app_advers);            ovalLayout = (LinearLayout) findViewById(R.id.ovalLayout);// 获取圆点容器            // 第二和第三参数 2选1 ,参数2为 图片网络路径数组 ,参数3为图片id的数组,本地测试用 ,2个参数都有优先采用 参数2            app_advers.start(this, mris, imageId, 3000, ovalLayout,                            R.drawable.dot_focused, R.drawable.dot_normal);            app_advers.setAdversOnItemClickListener(new AdGallery.AdversOnItemClickListener() {                public void onItemClick(int curIndex) {                    AppLog.error("点击的图片下标为:" + curIndex);                    // System.out.println(curIndex);                }            });        }        @Override        public void widgetClick(View v) {            Intent intent = null;             switch(v.getId()){            case R.id.tv_search:                intent = new Intent(HomeActivity.this,SearchActivity.class);                startActivity(intent);                break;            case R.id.tv_contact:                intent = new Intent(HomeActivity.this,MeActivity.class);                startActivity(intent);                break;            case R.id.iv_dia:                intent = new Intent(HomeActivity.this,DiacodeActivity.class);                startActivity(intent);                break;            }        }        @Override        public void widgetItemClick(AdapterView<?> parent, View view, int position,                long id) {        }    }

完成之后,我们看到,我们需要的第一个activity就完成了,至于那个广告Gallery我们后面讲。还有最终要的是别忘记在我们的AndroidMenifest.xml文件中定义我们的activity,在我们之前定义MarketTab的时候,我们也忘记在AndroidMenifest.xml文件中定义了。同时,我们需要把MarketTab设置为启动Activity。

我们看一下:

    <activity android:name="com.sdu.activities.MarketTab" >            <intent-filter>                <action android:name="android.intent.action.MAIN" >                </action>                <category android:name="android.intent.category.LAUNCHER" >                </category>            </intent-filter>        </activity>        <activity            android:name="com.sdu.activities.HomeActivity"            android:label="@string/app_name" >        </activity>

好了,这个完成之后,我们以同样的方式添加activity,并且所有的activity里面都不需要添加东西,同时,在我们HomeActivity里面点击头像之后进入个人主页面的监听,有搜索界面,有二维码界面,都需要添加上。

下面附上我的工程,大家可以打开看一下。

工程地质

1 0
原创粉丝点击