码迷,mamicode.com
首页 > 其他好文 > 详细

TabHost

时间:2018-05-22 14:53:50      阅读:221      评论:0      收藏:0      [点我收藏+]

标签:child   coding   override   public   oncreate   布局   his   appcompat   support   

逻辑

package com.example.administrator.hello;

import android.app.TabActivity;
import android.content.Intent;
import android.support.v4.view.LayoutInflaterCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ExpandableListView;
import android.widget.TabHost;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends TabActivity {
    private TabHost tabHost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tabHost=(TabHost)findViewById(R.id.tab);
        tabHost.addTab(tabHost.newTabSpec("TAB1").setIndicator("线性布局").setContent(R.id.lay1));
        tabHost.addTab(tabHost.newTabSpec("TAB2").setIndicator("绝对布局").setContent(R.id.lay2));
        tabHost.addTab(tabHost.newTabSpec("TAB3").setIndicator("相对布局").setContent(new Intent(MainActivity.this,A.class)));

    }
}

 

布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lay3">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hello 3"
        android:id="@+id/tv3"/>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lay1">
    <TextView
        android:text="111111111"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/lay2"
    >
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello word 2"/>
</AbsoluteLayout>
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:id="@+id/tab"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:id="@+id/lay1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TabWidget
            android:id="@+id/we"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </TabWidget>
<FrameLayout
    android:id="@+id/fra"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</FrameLayout>
    </LinearLayout>

</TabHost>

 

 

package com.example.administrator.hello;

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

/**
 * Created by Administrator on 2018/5/21.
 */
public class A extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.a);
    }
}

 

 

package com.example.administrator.hello;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/5/21.
 */
public class MyAdapter extends BaseExpandableListAdapter {
    private Context context;
    private List<String> listl;
    private Map<String,List<String>> map;


    MyAdapter( Context context, List<String> listl, Map<String,List<String>> map)
    {
        this.context=context;
        this.listl=listl;
        this.map=map;

    }

    @Override
    public int getGroupCount() {
        return listl.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        String key =listl.get(groupPosition);
        List list_c= map.get(key);
        return list_c.size();
    }

    @Override
    public Object getGroup(int groupPosition) {

        return listl.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        String key =listl.get(groupPosition);
        List list_c= map.get(key);
        return list_c;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if(convertView==null)
        {
             Viewh_p viewh_p=new Viewh_p();
            convertView= LayoutInflater.from(context).inflate(R.layout.tv_p,null);
            viewh_p.tv_p=(TextView)convertView.findViewById(R.id.tv_p);
            convertView.setTag(viewh_p);

        }
        else{


        }
        return null;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        return null;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}


class Viewh_p{
     TextView tv_p;
}

class Viewh_c{
     TextView tv_c;
}

 

TabHost

标签:child   coding   override   public   oncreate   布局   his   appcompat   support   

原文地址:https://www.cnblogs.com/excellencesy/p/9067237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!