Android儿童小组活动

朋友您好,今天我要分享有关“儿童小组活动”的重要帖子,表示该活动在“标签小组”中打开。 为此,我使用了动画活动。 我从stackoverflow.com中选择了此逻辑。 希望对您有所帮助。

打印屏幕:

  1. 创建一个新项目,命名为TabGroupChildDemo。
  2. 创建一个TabHostActivity并将其扩展到TabActivity。
  3. 创建其他3个活动名称-HomeActivity,AboutActivity,ContactActivity。
  4. 创建3个组活动名称-HomeGroupActivity,AboutGroupActivity,ContactGroupActivity。
  5. 创建另一个活动ChildActivity.java。
  6. 为首页,关于,联系人,子活动,名称activity_home,activity_about,activity_contact,activity_child创建4个布局。
  7. 为显示标签创建其他3种布局-home_tab,about_tab,contact_tab。
  8. 在manifest.xml中添加活动和组活动
  9. 在可绘制文件夹中添加图片– home.png,about.png,contact.png,ic_tab_background,下载以下图片

我的代码:

TabHostActivity.java

package com.manish.tabdemo;

import android.app.TabActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;



@SuppressWarnings("deprecation")
public class TabHostActivity extends TabActivity implements OnTabChangeListener
{ 
 private static final String[] TABS = { "HomeGroupActivity", "AboutGroupActivity", "ContactGroupActivity" };
 private static final String[] TAB_NAMES = { "Home", "About", "Contact"};
 public static TabHost tabs ;
    public static TabWidget tabWidget ;    
 protected Bitmap roundedImage;
    public boolean checkTabsListener = false;
 
    
    public void onCreate(Bundle icicle)
    {        
     super.onCreate(icicle);
        setContentView(R.layout.activity_tab_host);
      
        Bitmap roundedImage = BitmapFactory.decodeResource(getResources(),R.drawable.ic_tab_background);
        roundedImage = getRoundedCornerBitmap(roundedImage,3);
       
        tabs = getTabHost();
   
        tabWidget = tabs.getTabWidget();
                      
     tabs.setOnTabChangedListener(this);
        
     for (int i = 0; i < TABS.length; i++)
        {
         TabHost.TabSpec tab = tabs.newTabSpec(TABS[i]);
         
         //Asociating Components
         ComponentName oneActivity = new ComponentName("com.manish.tabdemo", "com.manish.tabdemo." + TABS[i]);
         Intent intent = new Intent().setComponent(oneActivity);           
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
         tab.setContent(intent);         
         //Setting the Indicator
         MyTabIndicator myTab = new MyTabIndicator(this, TAB_NAMES[i],(i+1), roundedImage); 
         tab.setIndicator(myTab); 
         tabs.addTab(tab);
        }
        
     checkTabsListener = true;
     
        for(int i=0;i<tabs.getTabWidget().getChildCount();i++)
        {
         tabs.getTabWidget().getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
        }
        
     
  tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);   
  
  //Maintaining Clicks
  
  // Home Tab Click
  
  tabWidget.getChildAt(0).setOnClickListener(new OnClickListener()
        {   
   @Override
   public void onClick(View v)
   {     
     if(HomeGroupActivity.HomeGroupStack != null && HomeGroupActivity.HomeGroupStack.mIdList.size()>1)
     {      
      HomeGroupActivity.HomeGroupStack.getLocalActivityManager().removeAllActivities();
      HomeGroupActivity.HomeGroupStack.mIdList.clear();
      HomeGroupActivity.HomeGroupStack.mIntents.clear();
      HomeGroupActivity.HomeGroupStack.mAnimator.removeAllViews();
      HomeGroupActivity.HomeGroupStack.startChildActivity("CareGroupActivity", new Intent(HomeGroupActivity.HomeGroupStack, HomeActivity.class));
      finish();
     }
     
     tabWidget.setCurrentTab(0);
     tabs.setCurrentTab(0);
     tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.ic_tab_background);  
   }
        });
  
 
  // About tab Click
  
  tabWidget.getChildAt(1).setOnClickListener(new OnClickListener()
        {   
   public void onClick(View v)
   {     
    if(AboutGroupActivity.AboutGroupStack != null && AboutGroupActivity.AboutGroupStack.mIdList.size()>0)
    {
     AboutGroupActivity.AboutGroupStack.getLocalActivityManager().removeAllActivities();
     AboutGroupActivity.AboutGroupStack.mIdList.clear();      
     AboutGroupActivity.AboutGroupStack.mIntents.clear();
     AboutGroupActivity.AboutGroupStack.mAnimator.removeAllViews();            
     AboutGroupActivity.AboutGroupStack.startChildActivity("TrackingGroupActivity", new Intent(AboutGroupActivity.AboutGroupStack, AboutActivity.class));           
    }     
             
    tabWidget.setCurrentTab(1);
    tabs.setCurrentTab(1);
    tabs.getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.ic_tab_background);       
   }
        });
  
  // Contact tab click
  
  tabWidget.getChildAt(2).setOnClickListener(new OnClickListener()
        {   
   public void onClick(View v)
   {     
    if(ContactGroupActivity.ContactGroupStack != null && ContactGroupActivity.ContactGroupStack.mIdList.size()>0)
    {
      
     ContactGroupActivity.ContactGroupStack.getLocalActivityManager().removeAllActivities();
     ContactGroupActivity.ContactGroupStack.mIdList.clear();      
     ContactGroupActivity.ContactGroupStack.mIntents.clear();
     ContactGroupActivity.ContactGroupStack.mAnimator.removeAllViews();            
     ContactGroupActivity.ContactGroupStack.startChildActivity("DashboardGroupActivity", new Intent(ContactGroupActivity.ContactGroupStack, ContactActivity.class));           
    }     
             
    tabWidget.setCurrentTab(2);
    tabs.setCurrentTab(2);
    tabs.getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.ic_tab_background);       
   }
        });
  

    }
  
    
    public class MyTabIndicator extends LinearLayout 
    {
  public MyTabIndicator(Context context, String label, int tabId, Bitmap bgImg)
  {
   super(context);
   LinearLayout tab = null;
   TextView tv;
   this.setGravity(Gravity.CENTER);
   
   if(tabId == 1)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.home_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }
   
   
   
   else if(tabId == 2)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.about_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }
   else if(tabId == 3)
   {
    tab = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.contact_tab, null);
    tv = (TextView)tab.findViewById(R.id.tab_label);
    tv.setText(label);
   }
       
   this.addView(tab, new LinearLayout.LayoutParams(320/4,55));   
  }  
    }

    
    
 public void onTabChanged(String tabId) 
 {      
  InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(tabs.getApplicationWindowToken(), 0);
          
        for(int i=0; i<tabs.getTabWidget().getChildCount(); i++)
  {                             
         if(tabId.equalsIgnoreCase(TABS[i]))
   {            
    tabs.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.ic_tab_background);     
   }
   else
   {
    tabs.getTabWidget().getChildAt(i).setBackgroundColor((Color.TRANSPARENT));
   }     
    }  
 }

  public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPxRadius)
 {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        
        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx =roundPxRadius;
     
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
     
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
     
        return output;
 }
 
 public void onResume()
 {
  super.onResume();
  
  
  //ReConstructing TabViews
  reDesignTabViews(); 
 }
 
 public void onPause()
 {
  super.onPause();     
 }
 

  
 
 /**
  * Method used to re constructing the Views at tab bar. This solves tabs disappearing issue.
  */
 public void reDesignTabViews()
 {
  MyTabIndicator myIndicator;
  
  
  //Construction of tab views....
  for(int i=0 ; i< tabWidget.getChildCount() ; i++)
  {
   myIndicator = (MyTabIndicator) tabWidget.getChildAt(i);
   myIndicator.removeAllViews();
   
   switch (i) 
   {

     case 0:
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.home_tab, null));
     break;
    case 1:    
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.about_tab, null));    
     break;
    case 2:    
     myIndicator.addView((LinearLayout) LayoutInflater.from(getApplicationContext()).inflate(R.layout.contact_tab, null));    
     break;
    
   }   
  }  
 }
 
}

HomeActivity.java

package com.manish.tabdemo;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.manish.util.AnimatedActivity;

public class HomeActivity extends Activity {
 Button button1;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_home);
  button1 = (Button) findViewById(R.id.button1);
  button1.setOnClickListener(new OnClickListener() {

    @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

     AnimatedActivity pActivity = (AnimatedActivity) HomeActivity.this
      .getParent();
    Intent intent = new Intent(HomeActivity.this,
      ChildActivity.class);
    pActivity.startChildActivity("home_screen", intent);
   }
  });
 }

  @Override
 public void onBackPressed() {
  System.out.println("***back*");
  HomeActivity.super.onBackPressed();
 }

  @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
  System.out.println("****event****" + event + "****" + keyCode);
  if (keyCode == KeyEvent.KEYCODE_BACK) {

    finish();
   return true;
  }
  return super.onKeyDown(keyCode, event);
 }
}

HomeGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class HomeGroupActivity extends AnimatedActivity {
 public static HomeGroupActivity HomeGroupStack;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  HomeGroupStack = HomeGroupActivity.this;

   startChildActivity("HomeGroupActivity", new Intent(this,
    HomeActivity.class));
 }
}

AboutActivity.java

package com.manish.tabdemo;

import android.app.Activity;
import android.os.Bundle;

public class AboutActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_about);
 }
}

AboutGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class AboutGroupActivity extends AnimatedActivity {
 public static AboutGroupActivity AboutGroupStack;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  AboutGroupStack = AboutGroupActivity.this;

   startChildActivity("AboutGroupActivity", new Intent(this,
    AboutActivity.class));
 }
}

ContactActivity.java

package com.manish.tabdemo;

import android.app.Activity;
import android.os.Bundle;

public class ContactActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_contact);
 }
}

ContactGroupActivity.java

package com.manish.tabdemo;

import android.content.Intent;
import android.os.Bundle;

import com.manish.util.AnimatedActivity;

public class ContactGroupActivity extends AnimatedActivity {
 public static ContactGroupActivity ContactGroupStack;

  @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ContactGroupStack = ContactGroupActivity.this;

   startChildActivity("ContactGroupActivity", new Intent(this,
    ContactActivity.class));
 }
}

ChildActivity.java

package com.manish.tabdemo;

import android.app.Activity;
import android.os.Bundle;

public class ChildActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_child);
 }
}

activity_tab_host.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    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="0dip"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="-4dp"
            android:layout_weight="0" />
    </LinearLayout>

</TabHost>

activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0099CC" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home Tab"
        android:textColor="#ffffff"
        android:textSize="35sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:text="Open Child" />

</RelativeLayout>

home_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/home" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Home"
        android:textColor="#0099CC" />

</LinearLayout>

activity_about.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="About Tab"
        android:textColor="#ffffff"
        android:textSize="35sp" />

</RelativeLayout>

about_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/about" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="About"
        android:textColor="#0099CC" />

</LinearLayout>

activity_contact.xml

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Contact Tab"
        android:textColor="#000000"
        android:textSize="35sp" />

</RelativeLayout>

contact_tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="1dip"
        android:layout_marginTop="2dip"
        android:background="@drawable/contact" >
    </TextView>

    <TextView
        android:id="@+id/tab_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Contact"
        android:textColor="#0099CC" />

</LinearLayout>

activity_child.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="#0099CC"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Child Activity"
        android:textSize="35sp"
        android:textColor="#ffffff" />

</RelativeLayout>

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.manish.tabdemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.manish.tabdemo.TabHostActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.manish.tabdemo.HomeActivity" />
        <activity android:name="com.manish.tabdemo.AboutActivity" />
        <activity android:name="com.manish.tabdemo.ContactActivity" />
        <activity android:name="com.manish.tabdemo.ChildActivity" />
        <activity android:name="com.manish.tabdemo.AboutGroupActivity" />
        <activity android:name="com.manish.tabdemo.ContactGroupActivity" />
        <activity android:name="com.manish.tabdemo.HomeGroupActivity" />
    </application>

</manifest>

您可以在下面的链接中找到“简单选项卡主机活动”的原始帖子– http://www.androidhub4you.com/2013/04/android-tabactivity-tab-layout-demo-tab.html

参考:来自我们的JCG合作伙伴 Manish Srivastava的Android儿童小组活动 ,位于您的Android Hub 4上。

翻译自: https://www.javacodegeeks.com/2013/04/android-child-group-activity.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大全,苹果公司所用字体大

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值