逻辑
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; }