macgg 发表于 2013-8-28 16:27

项目实战(新闻客户端)步步为营

9                              Main.xml   &lt;?xml&lt;LinearLayout    &lt;TabHost      &lt;LinearLayout            &lt;FrameLayout            &lt;/FrameLayout&gt;            &lt;TabWidget            &lt;RelativeLayout                &lt;RadioGroup                  &lt;RadioButton                  &lt;RadioButton                  &lt;RadioButton                  &lt;RadioButton                  &lt;RadioButton                &lt;/RadioGroup&gt;            &lt;/RelativeLayout&gt;      &lt;/LinearLayout&gt;    &lt;/TabHost&gt;&lt;/LinearLayout&gt; package com.and.netease;import com.and.netease.utils.MoveBg;import android.app.TabActivity;import android.content.Intent;import android.os.Bundle;import android.widget.RadioGroup;import android.widget.RadioGroup.OnCheckedChangeListener;import android.widget.ImageView;import android.widget.RelativeLayout;import android.widget.TabHost;public class MainActivity extends TabActivity {       TabHost tabHost;       TabHost.TabSpec tabSpec;       RadioGroup radioGroup;       RelativeLayoutbottom_layout;       ImageView img;       int startLeft;    @Override    public void onCreate(BundlesavedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.main);            bottom_layout =(RelativeLayout) findViewById(R.id.layout_bottom);            tabHost = getTabHost();         tabHost.addTab(tabHost.newTabSpec(&quot;news&quot;).setIndicator(&quot;News&quot;).setContent(newIntent(this, TabNewsActivity.class)));         tabHost.addTab(tabHost.newTabSpec(&quot;topic&quot;).setIndicator(&quot;Topic&quot;).setContent(newIntent(this, TabTopicActivity.class)));         tabHost.addTab(tabHost.newTabSpec(&quot;picture&quot;).setIndicator(&quot;Picture&quot;).setContent(newIntent(this, TabPicActivity.class)));         tabHost.addTab(tabHost.newTabSpec(&quot;follow&quot;).setIndicator(&quot;Follow&quot;).setContent(newIntent(this, TabFollowActivity.class)));         tabHost.addTab(tabHost.newTabSpec(&quot;vote&quot;).setIndicator(&quot;Vote&quot;).setContent(newIntent(this, TabVoteActivity.class)));            radioGroup =(RadioGroup) findViewById(R.id.radiogroup);         radioGroup.setOnCheckedChangeListener(checkedChangeListener);            img = newImageView(this);         img.setImageResource(R.drawable.tab_front_bg);      bottom_layout.addView(img);    }           privateOnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener(){            @Override            public voidonCheckedChanged(RadioGroup group, int checkedId) {                     switch(checkedId) {                     caseR.id.radio_news:                            tabHost.setCurrentTabByTag(&quot;news&quot;);//                         moveFrontBg(img,startLeft, 0, 0, 0);                            MoveBg.moveFrontBg(img,startLeft, 0, 0, 0);                            startLeft= 0;                            break;                     caseR.id.radio_topic:                            tabHost.setCurrentTabByTag(&quot;topic&quot;);                            MoveBg.moveFrontBg(img,startLeft, img.getWidth(), 0, 0);                            startLeft= img.getWidth();                            break;                     caseR.id.radio_pic:                            tabHost.setCurrentTabByTag(&quot;picture&quot;);                            MoveBg.moveFrontBg(img,startLeft, img.getWidth() * 2, 0, 0);                            startLeft= img.getWidth() * 2;                            break;                     caseR.id.radio_follow:                            tabHost.setCurrentTabByTag(&quot;follow&quot;);                            MoveBg.moveFrontBg(img,startLeft, img.getWidth() * 3, 0, 0);                            startLeft= img.getWidth() * 3;                            break;                     caseR.id.radio_vote:                            tabHost.setCurrentTabByTag(&quot;vote&quot;);                            MoveBg.moveFrontBg(img,startLeft, img.getWidth() * 4, 0, 0);                            startLeft= img.getWidth() * 4;                            break;                     default:                            break;                     }            }       };} package com.and.netease.utils;import android.view.View;importandroid.view.animation.TranslateAnimation;public class MoveBg {         /**          * 移动方法          *           * @param v          *             需要移动的View          * @param startX          *             起始x坐标          * @param toX          *             终止x坐标          * @param startY          *             起始y坐标          * @param toY          *             终止y坐标          */                           publicstatic void moveFrontBg(View view, int startX, int toX, int startY, int toY){                   TranslateAnimationanim = new TranslateAnimation(startX, toX, startY, toY);                   anim.setDuration(200);                   anim.setFillAfter(true);                   view.startAnimation(anim);         }} &lt;?xml&lt;LinearLayout    &lt;RelativeLayout      &lt;ImageView      &lt;TextView      &lt;ImageView    &lt;/RelativeLayout&gt;    &lt;RelativeLayout      &lt;LinearLayout            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;            &lt;RelativeLayout                &lt;TextView            &lt;/RelativeLayout&gt;      &lt;/LinearLayout&gt;    &lt;/RelativeLayout&gt;&lt;/LinearLayout&gt; package com.and.netease;import com.and.netease.utils.MoveBg;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.RelativeLayout;import android.widget.RelativeLayout.LayoutParams;import android.widget.TextView;public class TabNewsActivity extends Activity {       RelativeLayout layout;       TextView tv_front;//需要移动的View       TextView tv_bar_news;       TextView tv_bar_sport;       TextView tv_bar_play;       TextView tv_bar_finance;       TextView tv_bar_science;       TextView tv_bar_more;       int avg_width = 0;// 用于记录平均每个标签的宽度,移动的时候需要       @Override       protected voidonCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.layout_news);            initViews();       }       private void initViews(){            layout =(RelativeLayout) findViewById(R.id.layout_title_bar);            tv_bar_news =(TextView) findViewById(R.id.tv_title_bar_news);            tv_bar_sport =(TextView) findViewById(R.id.tv_title_bar_sport);            tv_bar_play =(TextView) findViewById(R.id.tv_title_bar_play);            tv_bar_finance =(TextView) findViewById(R.id.tv_title_bar_finance);            tv_bar_science =(TextView) findViewById(R.id.tv_title_bar_science);            tv_bar_more =(TextView) findViewById(R.id.tv_title_bar_more);            tv_bar_news.setOnClickListener(onClickListener);            tv_bar_sport.setOnClickListener(onClickListener);            tv_bar_play.setOnClickListener(onClickListener);            tv_bar_finance.setOnClickListener(onClickListener);            tv_bar_science.setOnClickListener(onClickListener);            tv_bar_more.setOnClickListener(onClickListener);            tv_front = newTextView(this);            tv_front.setBackgroundResource(R.drawable.slidebar);            tv_front.setTextColor(Color.WHITE);            tv_front.setText(&quot;头条&quot;);            tv_front.setGravity(Gravity.CENTER);            RelativeLayout.LayoutParamsparam = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);            param.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);            layout.addView(tv_front,param);       }       private OnClickListeneronClickListener = new OnClickListener() {            int startX;//移动的起始位置            @Override            public voidonClick(View v) {                     avg_width =findViewById(R.id.layout).getWidth();                     switch(v.getId()) {                     caseR.id.tv_title_bar_news:                            MoveBg.moveFrontBg(tv_front,startX, 0, 0, 0);                            startX= 0;                            tv_front.setText(R.string.title_news_category_tops);                            break;                     caseR.id.tv_title_bar_sport:                            MoveBg.moveFrontBg(tv_front,startX, avg_width, 0, 0);                            startX= avg_width;                            tv_front.setText(R.string.title_news_category_sport);                            break;                     caseR.id.tv_title_bar_play:                            MoveBg.moveFrontBg(tv_front,startX, avg_width * 2, 0, 0);                            startX= avg_width * 2;                            tv_front.setText(R.string.title_news_category_play);                            break;                     caseR.id.tv_title_bar_finance:                            MoveBg.moveFrontBg(tv_front,startX, avg_width * 3, 0, 0);                            startX= avg_width * 3;                            tv_front.setText(R.string.title_news_category_finance);                            break;                     caseR.id.tv_title_bar_science:                            MoveBg.moveFrontBg(tv_front,startX, avg_width * 4, 0, 0);                            startX= avg_width * 4;                            tv_front.setText(R.string.title_news_category_science);                            break;                     caseR.id.tv_title_bar_more:                            MoveBg.moveFrontBg(tv_front,startX, avg_width * 5, 0, 0);                            startX= avg_width * 5;                            tv_front.setText(R.string.title_news_category_more);                            break;                     default:                            break;                     }            }       };} tabActivity的功能。这里不再赘述。layout_topic.xml)   &lt;?xml&lt;LinearLayout    &lt;RelativeLayout      &lt;ImageView      &lt;TextView    &lt;/RelativeLayout&gt;&lt;/LinearLayout&gt; package com.and.netease;import android.app.Activity;import android.os.Bundle;public class TabTopicActivity extends Activity {       @Override       protected voidonCreate(Bundle savedInstanceState) {            // TODOAuto-generated method stub            super.onCreate(savedInstanceState);            setContentView(R.layout.layout_topic);       }} </div
页: [1]
查看完整版本: 项目实战(新闻客户端)步步为营