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

书接上回,在上面一片博客中,我们加入了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 = {
                "//img-my.csdn.net/uploads/201312/14/1386989803_3335.PNG",
                "//img-my.csdn.net/uploads/201312/14/1386989613_6900.jpg",
                "//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里面点击头像之后进入个人主页面的监听,有搜索界面,有二维码界面,都须要加入上。

以下附上我的project,大家能够打开看一下。

project地质

posted @ 2017-04-13 17:10  mfmdaoyou  阅读(221)  评论(0编辑  收藏  举报