26
2014
05

Android开发学习:切换菜单-使用TabHost控件,仿网易新闻客户端

Android开发学习

切换菜单,第一种:点击进行Tab切换,使用控件TabHost

新建xml布局文件:activity_main.xml,上面包含5个RadioButton按钮,代码内容:

<?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="vertical" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="0.0dip"
                android:layout_weight="1.0" >
            </FrameLayout>

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:visibility="gone" />

            <RelativeLayout
                android:id="@+id/layout_bottom"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <RadioGroup
                    android:id="@+id/radiogroup"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:background="@drawable/bottom_bg"
                    android:gravity="center_vertical"
                    android:orientation="horizontal" >

                    <RadioButton
                        android:id="@+id/radio_news"
                        android:layout_width="wrap_content"
                        android:layout_weight="1.0"
                        android:background="@drawable/tab_selector_1"
                        android:button="@null"
                        android:checked="true" />

                    <RadioButton
                        android:id="@+id/radio_topic"
                        android:layout_width="wrap_content"
                        android:layout_weight="1.0"
                        android:background="@drawable/tab_selector_2"
                        android:button="@null" />

                    <RadioButton
                        android:id="@+id/radio_pic"
                        android:layout_width="wrap_content"
                        android:layout_weight="1.0"
                        android:background="@drawable/tab_selector_3"
                        android:button="@null" />

                    <RadioButton
                        android:id="@+id/radio_follow"
                        android:layout_width="wrap_content"
                        android:layout_weight="1.0"
                        android:background="@drawable/tab_selector_4"
                        android:button="@null" />

                    <RadioButton
                        android:id="@+id/radio_vote"
                        android:layout_width="wrap_content"
                        android:layout_weight="1.0"
                        android:background="@drawable/tab_selector_5"
                        android:button="@null" />
                </RadioGroup>
            </RelativeLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>

使用线性布局LinearLayout,资源文件中的四个按钮使用布局图片文件tab_selector_1.xml、tab_selector_2.xmltab_selector_3.xml、tab_selector_4.xml、tab_selector_5.xml,在此就不一一列出

五个按钮点击后对应五个布局及页面:Tab1Activity.java、Tab2Activity.java、Tab3Activity.java、Tab4Activity.java、Tab5Activity.java;layout_1.xml、layout_2.xml、layout_3.xml、layout_4.xml、layout_5.xml,在此也不一一列出

实现MainActivity.java代码:

package com.zhengdecai.switchmenu;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.TabActivity.Tab1Activity;
import com.TabActivity.Tab2Activity;
import com.TabActivity.Tab3Activity;
import com.TabActivity.Tab4Activity;
import com.TabActivity.Tab5Activity;
import com.publicClass.MoveBg;

/**
 * 切换菜单:TabHost控件使用TabActivity,包括:Tab1Activity.java、Tab2Activity.java、Tab3Activity.java、Tab4Activity.java、Tab5Activity.java、MainActivity.java、MoveBag.java
 * ml文件包括:tab_selector_1 .xml、tab_selector_2.xml、tab_selector_3.xml、tab_selector_4.xml、tab_selector_5.xml、activitty_main.xml、layout_1.xml、layout_2.xml、layout_3.xml、layout_4.xml、layout_5.xml
 *
 * @author 郑德才
 *
 */
public class MainActivity extends TabActivity {
 TabHost tabHost;
 TabHost.TabSpec tabSpec;
 RadioGroup radioGroup;
 RelativeLayout bottom_layout;
 ImageView img;
 int startLeft;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  bottom_layout = (RelativeLayout) findViewById(R.id.layout_bottom);

  tabHost = getTabHost();
  tabHost.addTab(tabHost.newTabSpec("news").setIndicator("News")
    .setContent(new Intent(this, Tab1Activity.class)));
  tabHost.addTab(tabHost.newTabSpec("topic").setIndicator("Topic")
    .setContent(new Intent(this, Tab2Activity.class)));
  tabHost.addTab(tabHost.newTabSpec("picture").setIndicator("Picture")
    .setContent(new Intent(this, Tab3Activity.class)));
  tabHost.addTab(tabHost.newTabSpec("follow").setIndicator("Follow")
    .setContent(new Intent(this, Tab4Activity.class)));
  tabHost.addTab(tabHost.newTabSpec("vote").setIndicator("Vote")
    .setContent(new Intent(this, Tab5Activity.class)));

  radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
  radioGroup.setOnCheckedChangeListener(checkedChangeListener);

  img = new ImageView(this);

  img.setImageResource(R.drawable.tab_front_bg);

  WindowManager wm = this.getWindowManager();
  int width = wm.getDefaultDisplay().getWidth();
  // int height = wm.getDefaultDisplay().getHeight();

  int Height = bottom_layout.getLayoutParams().height;

  RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(
    width / 5, Height);
  param.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);

  bottom_layout.addView(img, param);
 }

 private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {

  @Override
  public void onCheckedChanged(RadioGroup group, int checkedId) {
   switch (checkedId) {
   case R.id.radio_news:
    tabHost.setCurrentTabByTag("news");
    // moveFrontBg(img, startLeft, 0, 0, 0);
    MoveBg.moveFrontBg(img, startLeft, 0, 0, 0);
    startLeft = 0;
    break;
   case R.id.radio_topic:
    tabHost.setCurrentTabByTag("topic");
    MoveBg.moveFrontBg(img, startLeft, img.getWidth(), 0, 0);
    startLeft = img.getWidth();
    break;
   case R.id.radio_pic:
    tabHost.setCurrentTabByTag("picture");
    MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 2, 0, 0);
    startLeft = img.getWidth() * 2;
    break;
   case R.id.radio_follow:
    tabHost.setCurrentTabByTag("follow");
    MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 3, 0, 0);
    startLeft = img.getWidth() * 3;
    break;
   case R.id.radio_vote:
    tabHost.setCurrentTabByTag("vote");
    MoveBg.moveFrontBg(img, startLeft, img.getWidth() * 4, 0, 0);
    startLeft = img.getWidth() * 4;
    break;

   default:
    break;
   }
  }
 };
}

实现效果:

 

« 上一篇下一篇 »

评论列表:

1.雪龙  2014/5/27 20:23:39 回复该留言
不错,就是有点简单!
2.压铸模具  2014/5/30 19:02:49 回复该留言
安卓发展很快。。。
3.宁波遮阳篷  2014/6/3 19:56:43 回复该留言
支持安卓。。。。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。