健康栏目

来源:互联网 发布:python 1 12 123 1234 编辑:程序博客网 时间:2024/04/29 23:10

一 效果

由于有错误 无法运行 

二 核心代码

1.HealthActivity.javapackage com.example.walkersimulate;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.annotation.SuppressLint;import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.support.v4.view.ViewPager;import android.support.v4.view.ViewPager.OnPageChangeListener;import android.util.DisplayMetrics;import android.view.Display;import android.view.View;import android.view.View.OnClickListener;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;@SuppressLint("NewApi")public class HealthActivity extends Activity{private int currIndex;// 当前页卡编号private TextView tvCursor;private TextView tvHealthNews;private TextView tvIllnessDefense;private ViewPager vpHealth;private ImageView ivHealthBack;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_health);initViews();initCursor();initViewPager();setListeners();}private void initViews(){ivHealthBack = (ImageView) findViewById(R.id.icons_health_back);tvCursor = (TextView) findViewById(R.id.cursor);tvHealthNews = (TextView) findViewById(R.id.tv_healthnews);tvIllnessDefense = (TextView) findViewById(R.id.tv_healthill);vpHealth = (ViewPager) findViewById(R.id.viewpager);}private void initCursor(){Display display = getWindow().getWindowManager().getDefaultDisplay();DisplayMetrics metrics = new DisplayMetrics();display.getMetrics(metrics);// 取得手机屏幕宽度的一半int tabLineLength = metrics.widthPixels / 2;// 设置游标的宽度为屏幕宽度的一半LayoutParams lp = (LayoutParams) tvCursor.getLayoutParams();lp.width = tabLineLength;tvCursor.setLayoutParams(lp);}private void initViewPager(){vpHealth = (ViewPager) findViewById(R.id.viewpager);List<View> views = new ArrayList<View>();views.add(new HealthWebView(this).getView("http://cms.hxky.cn/wap/jkxz/"));views.add(new HealthWebView(this).getView("http://cms.hxky.cn/wap/jbfz/"));vpHealth.setAdapter(new HealthViewPagerAdapter(views));// 给 ViewPager 设置适配器vpHealth.setCurrentItem(0);// 设置当前显示标签页为第一页}private void setListeners(){// 返回按钮ivHealthBack.setOnClickListener(new OnClickListener(){public void onClick(View arg0){finish();}});// 点击健康须知tvHealthNews.setOnClickListener(new OnClickListener(){public void onClick(View view){vpHealth.setCurrentItem(0);}});// 点击疾病防治tvIllnessDefense.setOnClickListener(new OnClickListener(){public void onClick(View view){vpHealth.setCurrentItem(1);}});vpHealth.setOnPageChangeListener(new OnPageChangeListener(){public void onPageSelected(int position){currIndex = position;}public void onPageScrolled(int position, float percent, int ag2){LinearLayout.LayoutParams ll = (android.widget.LinearLayout.LayoutParams) tvCursor.getLayoutParams();if (currIndex == position){ll.leftMargin = (int) (currIndex * tvCursor.getWidth() + percent* tvCursor.getWidth());} else if (currIndex > position){ll.leftMargin = (int) (currIndex * tvCursor.getWidth() - (1 - percent)* tvCursor.getWidth());}tvCursor.setLayoutParams(ll);}public void onPageScrollStateChanged(int position){}});}}2.HealthViewPagerAdapter.java
package com.example.walkersimulate;import java.util.List;import android.view.View;import android.view.ViewGroup;public class HealthViewPagerAdapter{private List<View> viewList;public HealthViewPagerAdapter(List<View> viewList){this.viewList = viewList;}public int getCount(){return viewList.size();}public boolean isViewFromObject(View view, Object object){return view == object;}public Object instantiateItem(ViewGroup container, int position){container.addView(viewList.get(position));return viewList.get(position);}public void destroyItem(ViewGroup container, int position, Object object){container.removeView(viewList.get(position));}}
3.HealthWebView.java
package com.example.walkersimulate;import android.annotation.SuppressLint;import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.webkit.WebView;import android.webkit.WebViewClient;public class HealthWebView{private Context context;public HealthWebView(Context context) {this.context = context;@SuppressLint("SetJavaScriptEnabled")public View getView(String url) {View view = LayoutInflater.from(context).inflate(R.layout.pagerofhealth, null);WebView webView = (WebView) view.findViewById(R.id.wvHealth);webView.loadUrl(url);// 设置支持 JavaScript 脚本webView.getSettings().setJavaScriptEnabled(true);/*** 禁止系统浏览器打开页面*/webView.setWebViewClient(new WebViewClient() {public boolean shouldOverrideUrlLoading(WebView view, String url) {view.loadUrl(url);return true;}});return view;}}
4.activity_health.xml
<LinearLayout 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:orientation="vertical"    tools:context=".HealthActivity" >    <include layout="@layout/health_layout_titlebar" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content" >        <!-- 健康新知 -->        <TextView            android:id="@+id/tv_healthnews"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:layout_weight="1"            android:gravity="center"            android:padding="6dp"            android:text="@string/healthnews"            android:textSize="18sp" />        <!-- 疾病防治 -->        <TextView            android:id="@+id/tv_healthill"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:layout_weight="1"            android:gravity="center"            android:padding="6dp"            android:text="@string/illdefense"            android:textSize="18sp" />    </LinearLayout>    <TextView        android:id="@+id/cursor"        android:layout_width="125dp"        android:layout_height="5dp"        android:layout_marginLeft="20dp"        android:background="#990033" />    <android.support.v4.view.ViewPager        android:id="@+id/viewpager"        android:layout_width="match_parent"        android:layout_height="match_parent" /></LinearLayout>
5.health_layout_titlebar.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:background="@drawable/titlebar_bg" >    <ImageView        android:id="@+id/icons_health_back"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:gravity="center"        android:paddingLeft="6dp"        android:src="@drawable/icons_health_back" />    <TextView        android:id="@+id/title"        android:layout_width="0dp"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:layout_weight="1"        android:gravity="center"        android:text="健康"        android:textColor="#ffffff"        android:textSize="15sp" /></LinearLayout>

三 总结 

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


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 荣耀4x返回键不管用怎么办 华为手机关不了机也开不了机怎么办 荣耀畅玩5x手机密码忘了怎么办 华为短信验证码失败其他错误怎么办 红米3x手机卡顿反应慢怎么办 华为手机不停的自动重启怎么办 华为荣耀4x卡在开机界面怎么办 华为荣耀8青春版密码忘了怎么办 华为手机内存满了开不起来怎么办 华为荣耀畅玩平板2比较卡怎么办 红米4x太卡了怎么办 魅蓝e移动卡只有2g网络怎么办 联通关闭2g网络双卡手机怎么办 小米6x手机插耳机音量大怎么办 音量键和亮度键盘拆了怎么办 荣耀v10锁屏密码忘了怎么办 荣耀9锁屏密码忘记了怎么办 华为荣耀v9锁屏密码忘记了怎么办 荣耀手机密码忘了怎么办数字锁 华为畅享5忘了开机密码怎么办 华为p9连接热点忘了密码怎么办 苹果手机用联通卡信号不好怎么办 魅族手机充电口松了怎么办 华为手机刷机失败开不了机怎么办 华为刷机失败开不了机怎么办 银行卡信息被盗密码被改该怎么办 全民k歌手机话筒有杂音怎么办 手机刷机清除数据需要密码怎么办 oppo手机屏锁密码忘了怎么办 华为手机摔了一下开不了机怎么办 华为v9手机删除隐私空间了怎么办 华为荣耀畅玩5x卡顿怎么办 淘宝买家收到货后恶意退款怎么办 手机淘宝申请退款后不想退了怎么办 买房交首付时的收据发票掉了怎么办 苹果商城消费提示问题忘记了怎么办 psd文件超过2g不能存储怎么办 手机拍的照片做微信头像太大怎么办 上传的照片在等待中传不上去怎么办 淘宝购物车里的图片模糊怎么办 天猫超市一箱饮料少两瓶怎么办