健康栏目

一 效果

由于有错误 无法运行 

二 核心代码

[html]  view plain  copy
  1. 1.HealthActivity.java  
  2. package com.example.walkersimulate;  
  3.   
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import android.os.Bundle;  
  8. import android.annotation.SuppressLint;  
  9. import android.app.ActionBar.LayoutParams;  
  10. import android.app.Activity;  
  11. import android.support.v4.view.ViewPager;  
  12. import android.support.v4.view.ViewPager.OnPageChangeListener;  
  13. import android.util.DisplayMetrics;  
  14. import android.view.Display;  
  15. import android.view.View;  
  16. import android.view.View.OnClickListener;  
  17. import android.widget.ImageView;  
  18. import android.widget.LinearLayout;  
  19. import android.widget.TextView;  
  20.   
  21. @SuppressLint("NewApi")  
  22. public class HealthActivity extends Activity  
  23. {  
  24.     private int currIndex;// 当前页卡编号  
  25.     private TextView tvCursor;  
  26.     private TextView tvHealthNews;  
  27.     private TextView tvIllnessDefense;  
  28.     private ViewPager vpHealth;  
  29.     private ImageView ivHealthBack;  
  30.   
  31.     @Override  
  32.     protected void onCreate(Bundle savedInstanceState)  
  33.     {  
  34.         super.onCreate(savedInstanceState);  
  35.         setContentView(R.layout.activity_health);  
  36.         initViews();  
  37.         initCursor();  
  38.         initViewPager();  
  39.         setListeners();  
  40.     }  
  41.   
  42.     private void initViews()  
  43.     {  
  44.         ivHealthBack = (ImageView) findViewById(R.id.icons_health_back);  
  45.         tvCursor = (TextView) findViewById(R.id.cursor);  
  46.         tvHealthNews = (TextView) findViewById(R.id.tv_healthnews);  
  47.         tvIllnessDefense = (TextView) findViewById(R.id.tv_healthill);  
  48.         vpHealth = (ViewPager) findViewById(R.id.viewpager);  
  49.     }  
  50.   
  51.     private void initCursor()  
  52.     {  
  53.         Display display = getWindow().getWindowManager().getDefaultDisplay();  
  54.         DisplayMetrics metrics = new DisplayMetrics();  
  55.         display.getMetrics(metrics);  
  56.         // 取得手机屏幕宽度的一半  
  57.         int tabLineLength = metrics.widthPixels / 2;  
  58.         // 设置游标的宽度为屏幕宽度的一半  
  59.         LayoutParams lp = (LayoutParams) tvCursor.getLayoutParams();  
  60.         lp.width = tabLineLength;  
  61.         tvCursor.setLayoutParams(lp);  
  62.     }  
  63.   
  64.     private void initViewPager()  
  65.     {  
  66.         vpHealth = (ViewPager) findViewById(R.id.viewpager);  
  67.         List<View> views = new ArrayList<View>();  
  68.         views.add(new HealthWebView(this)  
  69.                 .getView("http://cms.hxky.cn/wap/jkxz/"));  
  70.         views.add(new HealthWebView(this)  
  71.                 .getView("http://cms.hxky.cn/wap/jbfz/"));  
  72.         vpHealth.setAdapter(new HealthViewPagerAdapter(views));  
  73.         // 给 ViewPager 设置适配器  
  74.         vpHealth.setCurrentItem(0);// 设置当前显示标签页为第一页  
  75.     }  
  76.   
  77.     private void setListeners()  
  78.     {  
  79.         // 返回按钮  
  80.         ivHealthBack.setOnClickListener(new OnClickListener()  
  81.         {  
  82.             public void onClick(View arg0)  
  83.             {  
  84.                 finish();  
  85.             }  
  86.         });  
  87.         // 点击健康须知  
  88.         tvHealthNews.setOnClickListener(new OnClickListener()  
  89.         {  
  90.             public void onClick(View view)  
  91.             {  
  92.                 vpHealth.setCurrentItem(0);  
  93.             }  
  94.         });  
  95.         // 点击疾病防治  
  96.         tvIllnessDefense.setOnClickListener(new OnClickListener()  
  97.         {  
  98.             public void onClick(View view)  
  99.             {  
  100.                 vpHealth.setCurrentItem(1);  
  101.             }  
  102.         });  
  103.         vpHealth.setOnPageChangeListener(new OnPageChangeListener()  
  104.         {  
  105.             public void onPageSelected(int position)  
  106.             {  
  107.                 currIndex = position;  
  108.             }  
  109.   
  110.             public void onPageScrolled(int position, float percent, int ag2)  
  111.             {  
  112.                 LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tvCursor  
  113.                         .getLayoutParams();  
  114.                 if (currIndex == position)  
  115.                 {  
  116.                     ll.leftMargin = (int) (currIndex * tvCursor.getWidth() + percent  
  117.                             * tvCursor.getWidth());  
  118.                 } else if (currIndex > position)  
  119.                 {  
  120.                     ll.leftMargin = (int) (currIndex * tvCursor.getWidth() - (1 - percent)  
  121.                             * tvCursor.getWidth());  
  122.                 }  
  123.                 tvCursor.setLayoutParams(ll);  
  124.             }  
  125.   
  126.             public void onPageScrollStateChanged(int position)  
  127.             {  
  128.             }  
  129.         });  
  130.     }  
  131. }  
  132. 2.HealthViewPagerAdapter.java  
[html]  view plain  copy
  1. package com.example.walkersimulate;  
  2.   
  3.   
  4. import java.util.List;  
  5.   
  6.   
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9.   
  10.   
  11. public class HealthViewPagerAdapter  
  12. {  
  13. <span style="white-space:pre">  </span>private List<View> viewList;  
  14.   
  15.   
  16. <span style="white-space:pre">  </span>public HealthViewPagerAdapter(List<View> viewList)  
  17. <span style="white-space:pre">  </span>{  
  18. <span style="white-space:pre">      </span>this.viewList = viewList;  
  19. <span style="white-space:pre">  </span>}  
  20.   
  21.   
  22. <span style="white-space:pre">  </span>public int getCount()  
  23. <span style="white-space:pre">  </span>{  
  24. <span style="white-space:pre">      </span>return viewList.size();  
  25. <span style="white-space:pre">  </span>}  
  26.   
  27.   
  28. <span style="white-space:pre">  </span>public boolean isViewFromObject(View view, Object object)  
  29. <span style="white-space:pre">  </span>{  
  30. <span style="white-space:pre">      </span>return view == object;  
  31. <span style="white-space:pre">  </span>}  
  32.   
  33.   
  34. <span style="white-space:pre">  </span>public Object instantiateItem(ViewGroup container, int position)  
  35. <span style="white-space:pre">  </span>{  
  36. <span style="white-space:pre">      </span>container.addView(viewList.get(position));  
  37. <span style="white-space:pre">      </span>return viewList.get(position);  
  38. <span style="white-space:pre">  </span>}  
  39.   
  40.   
  41. <span style="white-space:pre">  </span>public void destroyItem(ViewGroup container, int position, Object object)  
  42. <span style="white-space:pre">  </span>{  
  43. <span style="white-space:pre">      </span>container.removeView(viewList.get(position));  
  44. <span style="white-space:pre">  </span>}  
  45. }  
[html]  view plain  copy
  1. 3.HealthWebView.java  
[html]  view plain  copy
  1. package com.example.walkersimulate;  
  2.   
  3.   
  4. import android.annotation.SuppressLint;  
  5. import android.content.Context;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.webkit.WebView;  
  9. import android.webkit.WebViewClient;  
  10.   
  11.   
  12. public class HealthWebView{  
  13. <span style="white-space:pre">  </span>private Context context;  
  14. <span style="white-space:pre">  </span>public HealthWebView(Context context) {  
  15. <span style="white-space:pre">  </span>this.context = context;  
  16. <span style="white-space:pre">  </span>}   
  17. <span style="white-space:pre">  </span>@SuppressLint("SetJavaScriptEnabled")  
  18. <span style="white-space:pre">  </span>public View getView(String url) {  
  19. <span style="white-space:pre">  </span>View view = LayoutInflater.from(context).inflate(  
  20. <span style="white-space:pre">  </span>R.layout.pagerofhealth, null);  
  21. <span style="white-space:pre">  </span>WebView webView = (WebView) view.findViewById(R.id.wvHealth);  
  22. <span style="white-space:pre">  </span>webView.loadUrl(url);  
  23. <span style="white-space:pre">  </span>// 设置支持 JavaScript 脚本  
  24. <span style="white-space:pre">  </span>webView.getSettings().setJavaScriptEnabled(true);  
  25. <span style="white-space:pre">  </span>/**  
  26. <span style="white-space:pre">  </span>* 禁止系统浏览器打开页面  
  27. <span style="white-space:pre">  </span>*/  
  28. <span style="white-space:pre">  </span>webView.setWebViewClient(new WebViewClient() {  
  29. <span style="white-space:pre">  </span>public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  30. <span style="white-space:pre">  </span>view.loadUrl(url);  
  31. <span style="white-space:pre">  </span>return true;  
  32. <span style="white-space:pre">  </span>}  
  33. <span style="white-space:pre">  </span>});  
  34. <span style="white-space:pre">  </span>return view;  
  35. <span style="white-space:pre">  </span>}  
  36. }  
[html]  view plain  copy
  1. 4.activity_health.xml  
[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     tools:context=".HealthActivity" >  
  7.   
  8.   
  9.     <include layout="@layout/health_layout_titlebar" />  
  10.   
  11.   
  12.     <LinearLayout  
  13.         android:layout_width="match_parent"  
  14.         android:layout_height="wrap_content" >  
  15.   
  16.   
  17.         <!-- 健康新知 -->  
  18.   
  19.   
  20.         <TextView  
  21.             android:id="@+id/tv_healthnews"  
  22.             android:layout_width="wrap_content"  
  23.             android:layout_height="wrap_content"  
  24.             android:layout_gravity="center"  
  25.             android:layout_weight="1"  
  26.             android:gravity="center"  
  27.             android:padding="6dp"  
  28.             android:text="@string/healthnews"  
  29.             android:textSize="18sp" />  
  30.         <!-- 疾病防治 -->  
  31.   
  32.   
  33.         <TextView  
  34.             android:id="@+id/tv_healthill"  
  35.             android:layout_width="wrap_content"  
  36.             android:layout_height="wrap_content"  
  37.             android:layout_gravity="center"  
  38.             android:layout_weight="1"  
  39.             android:gravity="center"  
  40.             android:padding="6dp"  
  41.             android:text="@string/illdefense"  
  42.             android:textSize="18sp" />  
  43.     </LinearLayout>  
  44.   
  45.   
  46.     <TextView  
  47.         android:id="@+id/cursor"  
  48.         android:layout_width="125dp"  
  49.         android:layout_height="5dp"  
  50.         android:layout_marginLeft="20dp"  
  51.         android:background="#990033" />  
  52.   
  53.   
  54.     <android.support.v4.view.ViewPager  
  55.         android:id="@+id/viewpager"  
  56.         android:layout_width="match_parent"  
  57.         android:layout_height="match_parent" />  
  58.   
  59.   
  60. </LinearLayout>  
[html]  view plain  copy
  1. 5.health_layout_titlebar.xml  
[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="wrap_content"  
  4.     android:background="@drawable/titlebar_bg" >  
  5.   
  6.   
  7.     <ImageView  
  8.         android:id="@+id/icons_health_back"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_gravity="center"  
  12.         android:gravity="center"  
  13.         android:paddingLeft="6dp"  
  14.         android:src="@drawable/icons_health_back" />  
  15.   
  16.   
  17.     <TextView  
  18.         android:id="@+id/title"  
  19.         android:layout_width="0dp"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_gravity="center"  
  22.         android:layout_weight="1"  
  23.         android:gravity="center"  
  24.         android:text="健康"  
  25.         android:textColor="#ffffff"  
  26.         android:textSize="15sp" />  
  27.   
  28.   
  29. </LinearLayout>  
[html]  view plain  copy
  1.   

三 总结 

通过这次练习,学会了一些UI布局的应用,让UI页面更加的多彩 ,虽然没能成功达到实验预想的效果,但也学到了一些方法和流程。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值