一个基于Android系统的新闻客户端(一)

    一、整体概述

      在服务器端,通过对凤凰网的抓取存入数据库,客户端通过向服务器发送请求得到新闻。

      服务端用WCF,宿主为window服务,客户端为Java写的安卓程序。

    二、客户端

      我在eclipse里新建了个Android项目,命名为MyNewClient,eclipse自动生成二个xml布局文件,如图:

      

      其中,fragment_main.xml是新版的布局文件,暂时不会用,把它删掉。

      新建xml文件,命名为activity_foot.xml,在这里我们要做app的底部,先上代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:background="#000000" >

    <LinearLayout
        android:id="@+id/home"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/home_bottom_home_bn"
            android:layout_width="28dip"
            android:layout_height="28dip"
            android:layout_gravity="center"
            android:background="@drawable/bottom_home_bn_style"
            />
        <TextView
            android:id="@+id/home_bottom_home_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingTop="2dip"
            android:textColor="@android:color/white"
            android:layout_gravity="center"
            android:text="@string/sy" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/edit"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/home_ediit_home_bn"
            android:layout_width="28dip"
            android:layout_height="28dip"
            android:layout_gravity="center"
            android:background="@drawable/bottom_edit_bn_style"
            />
        <TextView
            android:id="@+id/home_bottom_edit_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingTop="2dip"
            android:textColor="@android:color/white"
            android:layout_gravity="center"
            android:text="@string/bj" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/xj"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/home_bottom_x_bn"
            android:layout_width="28dip"
            android:layout_height="28dip"
            android:layout_gravity="center"
            android:background="@drawable/bottom_xianji_bn_style"
            />
        <TextView
            android:id="@+id/home_bottom_x_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingTop="2dip"
            android:textColor="@android:color/white"
            android:layout_gravity="center"
            android:text="@string/xj" />
    </LinearLayout>

     <LinearLayout
        android:id="@+id/shop"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/home_bottom_xj_bn"
            android:layout_width="28dip"
            android:layout_height="28dip"
            android:layout_gravity="center"
            android:background="@drawable/bottom_shop_bn_style"
            />
        <TextView
            android:id="@+id/home_bottom_xj_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingTop="2dip"
            android:textColor="@android:color/white"
            android:layout_gravity="center"
            android:text="@string/xj" />
    </LinearLayout>

     <LinearLayout
        android:id="@+id/show"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical"
        >
        <ImageView
            android:id="@+id/bottom_show_bn_style_bn"
            android:layout_width="28dip"
            android:layout_height="28dip"
            android:layout_gravity="center"
            android:background="@drawable/bottom_show_bn_style"
            />
        <TextView
            android:id="@+id/bottom_show_bn_style_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:textSize="12sp"
            android:textStyle="bold"
            android:paddingTop="2dip"
            android:textColor="@android:color/white"
            android:layout_gravity="center"
            android:text="@string/xj" />
    </LinearLayout>

</LinearLayout>

       其中用到了几章图片,,,,,,,,,,

       其中有的属性中有用到xml,用来定义单击事件发生时的状态得变化,下面是其一个的代码,其他代码秩序修改其变化后的图片即可。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:state_window_focused="false" android:drawable="@drawable/bottom_looks"></item>
    <item android:state_selected="true" android:drawable="@drawable/bottom_looks_d"></item>
    <item android:state_pressed="true" android:drawable="@drawable/bottom_looks_d"></item>
    <item android:state_focused="true" android:drawable="@drawable/bottom_looks_d"></item>
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/bottom_home_d"></item>

</selector>

         新建xml文件,命名为activity_main.xml,在这里我们要对app进行布局,它包含app的头,和底部,还有一个WebView,下面是代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/top"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" >

        <include layout="@layout/activity_top" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/web"
        android:layout_width="match_parent"
        android:layout_height="420dp"
        android:layout_alignParentBottom="true" >

        <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <include layout="@layout/activity_foot" />
    </LinearLayout>

</RelativeLayout>

       其中include用于包含xml文件。

        新建xml文件,命名为activity_top.xml,在这里我们要做app的头部,下面是代码:  

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:orientation="horizontal"
    android:background="@drawable/top_bg">"

  <LinearLayout
        android:id="@+id/home_top"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"

        android:orientation="vertical">
  <TextView
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      android:text="@string/home"
      android:textColor="#ffffff"
      android:layout_gravity="center"
      android:textSize="25sp" />
  </LinearLayout>

  <LinearLayout
        android:id="@+id/sh_top"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
  <TextView
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      android:text="@string/socity"
      android:textColor="#ffffff"
      android:layout_gravity="center"
      android:textSize="25sp" />
  </LinearLayout>

  <LinearLayout
        android:id="@+id/zz_top"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
  <TextView
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      android:text="@string/政治"
      android:textColor="#ffffff"
      android:layout_gravity="center"
      android:textSize="25sp" />
  </LinearLayout>

  <LinearLayout
        android:id="@+id/kj_top"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
  <TextView
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      android:text="@string/kj"
      android:textColor="#ffffff"
      android:layout_gravity="center"
      android:textSize="25sp" />
  </LinearLayout>

  <LinearLayout
        android:id="@+id/js_top"
        android:layout_width="64dp"
        android:layout_height="match_parent"
        android:layout_marginTop="4dip"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">
  <TextView
      android:layout_height="match_parent"
      android:layout_width="wrap_content"
      android:text="@string/js"
      android:textColor="#ffffff"
      android:layout_gravity="center"
      android:textSize="25sp" />
  </LinearLayout>

</LinearLayout>

       布局文件基本完毕.

       下面是java代码:

package com.example.liguifa;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.Display;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewTreeObserver;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.os.Build;

@SuppressLint("SetJavaScriptEnabled") public class MainActivity extends ActionBarActivity implements OnClickListener {
    private WebView wv;
    public int topHeight;
    public int bottomHeight;
    private int W;
    private int H;
    public LinearLayout lineTop;
    public LinearLayout lineBottom;
    private LinearLayout webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.window_Load();
        this.LoadWeb();
    }

    public void LoadWeb()
    {
        wv = (WebView)findViewById(R.id.webView1);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setScrollBarStyle(0);
        WebSettings webSettings = wv.getSettings();
        webSettings.setAllowFileAccess(true);
        webSettings.setBuiltInZoomControls(true);
        wv.loadUrl("http://xw.qq.com");
        //加载数据
        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress)
            {
                if (newProgress == 100)
                {
                    MainActivity.this.setTitle("加载完成");
                }
                else
                {
                    MainActivity.this.setTitle("加载中.......");
                }
            }
       });
    }

    private boolean window_Load()
    {
        Display mDisplay = getWindowManager().getDefaultDisplay();
        W = mDisplay.getWidth();
        H = mDisplay.getHeight();
        int foot_Ico=W/5;
        LinearLayout[] l=new LinearLayout[]{
                (LinearLayout)this.findViewById(R.id.home),
                (LinearLayout)this.findViewById(R.id.edit),
                (LinearLayout)this.findViewById(R.id.xj),
                (LinearLayout)this.findViewById(R.id.shop),
                (LinearLayout)this.findViewById(R.id.home_top),
                (LinearLayout)this.findViewById(R.id.sh_top),
                (LinearLayout)this.findViewById(R.id.zz_top),
                (LinearLayout)this.findViewById(R.id.kj_top),
                (LinearLayout)this.findViewById(R.id.js_top)
        };
        for(LinearLayout line:l)
        {
            LayoutParams laParams=(LayoutParams)line.getLayoutParams();
            laParams.width=foot_Ico;
            line.setLayoutParams(laParams);
            line.setOnClickListener(this);
        }
        webView=(LinearLayout)this.findViewById(R.id.web);
        lineTop=((LinearLayout)this.findViewById(R.id.top));
        lineBottom=((LinearLayout)this.findViewById(R.id.bottom));
        ViewTreeObserver vto = lineTop.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){

            @Override
            public boolean onPreDraw() {
                // TODO 自动生成的方法存根

                    topHeight = lineTop.getHeight();

                    ViewTreeObserver vto1= lineBottom.getViewTreeObserver();
                    vto1.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){

                        @Override
                        public boolean onPreDraw() {
                            // TODO 自动生成的方法存根

                                bottomHeight = lineBottom.getHeight();

                                LayoutParams laParams=(LayoutParams)webView.getLayoutParams();
                                laParams.height=H-topHeight-bottomHeight-24;
                                webView.setLayoutParams(laParams);

                                return true;

                        }

                    });

                    return true;

            }

        });

        return true;
    }

    @Override
    public void onClick(View arg0) {
        // TODO 自动生成的方法存根
//        new AlertDialog.Builder(this).setTitle("提示").setMessage("正在测试").show();
//        setContentView(R.layout.activity_edit);
//        this.window_Load();
    }
}

       其中,LoadWeb();用于自适应屏幕的大小,

        Display mDisplay = getWindowManager().getDefaultDisplay();

        W = mDisplay.getWidth();
        H = mDisplay.getHeight();

       可以得到屏幕的分辨率宽W,高H。

        ViewTreeObserver类可以动态改变控件大小。

        方法为

        ViewTreeObserver vto = lineTop(控件句柄).getViewTreeObserver();

        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener(){        

          @Override
          public boolean onPreDraw() {
                               topHeight(int型变量) = lineTop.getHeight();

            return true;

          });

       WebView的使用方法为:       

       wv = (WebView)findViewById(R.id.webView1);
       wv.getSettings().setJavaScriptEnabled(true);
       wv.setScrollBarStyle(0);
                    WebSettings webSettings = wv.getSettings();
                    webSettings.setAllowFileAccess(true);
                    webSettings.setBuiltInZoomControls(true);
                    wv.loadUrl("http://xw.qq.com");
                   //加载数据
                  wv.setWebChromeClient(new WebChromeClient() {
                 @Override
                 public void onProgressChanged(WebView view, int newProgress)
                {
                if (newProgress == 100)
               {
               MainActivity.this.setTitle("加载完成");
               }
              else
               {
                     MainActivity.this.setTitle("加载中.......");
               }
               }
               });
               }

/*********************************************这是一个学习笔记****************************************************************/

     总结:由于Java不是强项,Android也刚学没几天,代码还有很多不算太懂的东西。

        

      

   

一个基于Android系统的新闻客户端(一),布布扣,bubuko.com

时间: 2024-08-02 06:51:18

一个基于Android系统的新闻客户端(一)的相关文章

分享基于Android系统的XMPP即时通讯技术项目实战(仿微信开发架构,自定义控件)

即时通讯技术是 网络技术中的明珠.即时通讯产品改变了人们的生活习惯.经过十几年的发展,即时通讯功能的应用也逐渐从专门软件才能实现的功能逐渐成为标准配置.即时通讯技术的应用不仅仅只是用于社交领域,在大多的协同工作领域,以及需要分布结构的网络通讯领域都是非常重要的.讲师本人在android领域有多年的开发经验,在即时通讯系统方面也有深入的研究.在本课程中,详细讲解了android开发中要解决的一些主要的问题,即时通讯在客户端实现的过程,并手把手的进行操作,方便学习和理解.因为课时的原因,对于一些初级

android 仿网易新闻客户端源码都有

原文:android 仿网易新闻客户端源码都有 android 仿网易新闻服务端源码 源代码下载地址: http://www.zuidaima.com/share/1550463560944640.htm http://www.zuidaima.com/share/1550463561206784.htm android 仿网易新闻 客户端和服务端 源码都有 ,有些功能还未实现,因为文件有点大,所以分为2次上传  java源代码截图:

Android实现网易新闻客户端侧滑菜单(二)

前面已经讲过通过三方开源库SlideMenu来实现这种效果,请参考Android实现网易新闻客户端侧滑菜单(一) 今天通过自定义View来实现这种功能 代码如下: SlideMenu.java <span style="font-family:SimSun;font-size:14px;">package com.jackie.slidemenu.view; import android.content.Context; import android.graphics.Ca

基于Android系统应用层与框架层剖析View点击事件

基于Android操作系统的框架层和应用层,介绍了View的绘制.触摸事件的传递流程,分析了View与用户交互时被回调的相关框架层代码和应用层代码,研究了Android应用中事件处理的相关重要机制.通过具体代码详细剖析了在Android系统下用户和View交互过程中折射出的回调机制,回调方法在系统框架的详细执行过程,以及基于回调机制的经典事件处理模型. 1 引言 Android是一种基于Linux的自由及开放源代码的操作系统,目前基于Android平台的应用日趋广泛.Android应用程序大多基

android之简易新闻客户端

将一个新闻信息保存到一个XML文件中,并将放在服务器下.通过手机客户端来从服务器下载该文件并解析显示. news.xml <?xml version="1.0" encoding="UTF-8" ?> <newslist> <news> <title>黑马52期就业快报</title> <detail>热烈祝贺黑马52期平均薪水突破13k</detail> <comment&g

Android案例之新闻客户端服务器实现,完全属于自己的新闻展示平台

一.新闻的数据库的构建 脚本代码如下:(使用的mysql5.0 数据库) SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET time_zone = "+00:00"; -- Database: `newsdemo` -- 表的结构 `news` CREATE TABLE IF NOT EXISTS `news` ( `id` int(10) NOT NULL AUTO_INCREMENT, `title` text NOT NU

一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(五)用户接口层之提取媒体流数据

当RTSP客户端向RTSP服务端发送完PLAY命令后,RTSP服务端就会另外开启UDP端口(SDP协商定义的端口)发送RTP媒体流数据包.这些数据包之间会间隔一段时间(毫秒级)陆续被发送到RTSP客户端,此时RTSP客户端可以调用GetMediaData等接口获取媒体流数据. 一.uint8_t * RtspClient::GetMediaData(string media_type, uint8_t * buf, size_t * size, size_t max_size) 该函数的作用即获

一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient):(二)示例

一.搭建RTSP服务器 要想测试RTSP客户端,没有服务端怎么行呢?然而,有时候条件有限,手头并没有独立的RTSP服务器拿来用,那么我们不妨自己撘一个. 以下有2种方便的做法可供选择: 第一种:使用vlc播放器(推荐做法) 这种方法最方便,而且任何视频格式的文件都可以拿来测试,具体做法网上有很多,以下提供一个链接以供参考. http://blog.csdn.net/beitiandijun/article/details/9232405 这种方法只有一个地方需要注意一下: 如果你想让你的vlc

一个基于JRTPLIB的轻量级RTSP客户端(myRTSPClient)——实现篇:(六)RTP音视频传输解析层之音视频数据传输格式

一.差异 本地音视频数据格式和用来传输的音视频数据格式存在些许差异,由于音视频数据流到达客户端时,需要考虑数据流的数据边界.分包.组包顺序等问题,所以传输中的音视频数据往往会多一些字节. 举个例子,有时候一个媒体分包数据量很大(比如H264的一个分包常常会有2-4K),而大多数网络的MTU(最大传输单元)基本都是1500字节. 如果频繁收发这么大的数据包,会额外增添路由器的负担,甚至会导致网络阻塞,不利于网络的稳定. 于是服务器就自行对H264进行了分包以适应MTU,每个分包的开始处往往会多出一