android fragmenttabhost 使用之二
来源:互联网 发布:win10一键优化工具 编辑:程序博客网 时间:2024/06/02 03:44
效果图如下
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="net.sytm.fragmenttabhost.MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <android.support.v4.app.FragmentTabHost android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tab_host"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" /> </android.support.v4.app.FragmentTabHost> <FrameLayout android:id="@+id/real_tab_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout></RelativeLayout>
line_selector.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true" android:drawable="@drawable/red_line" /> <item android:drawable="@drawable/blue_line" /></selector>
red_line.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="@android:color/holo_blue_dark" /></shape>
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="line"> <stroke android:width="1dp" android:color="@android:color/holo_red_dark" /></shape>
tab_view.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" android:gravity="center"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:layout_margin="8dp" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" tools:text="考勤" android:id="@+id/tab_text" android:textColor="@color/colorTabTextBg" android:textSize="10sp"/> <ImageView android:layout_width="16dp" android:layout_height="8dp" tools:src="@drawable/line_selector" android:id="@+id/tab_image" /> </LinearLayout></LinearLayout>
MainActivity.java
package net.sytm.fragmenttabhost;import android.support.v4.app.FragmentTabHost;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends AppCompatActivity { private int[] tabImage; private String[] tabTitle; private FragmentTabHost tabHost; private Class[] fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //图标 tabImage = new int[]{R.drawable.tab_check_in_btn, R.drawable.tab_calender_btn, R.drawable.tab_msg_btn, R.drawable.tab_tools_btn}; //标题 tabTitle = new String[]{"考勤", "日程", "消息", "工具"}; fragment = new Class[]{CheckInFragment.class, CalenderFragment.class, MessageFragment.class, ToolsFragment.class, }; tabHost = (FragmentTabHost) findViewById(R.id.tab_host); tabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content); for (int i = 0; i < tabTitle.length; i++) { FragmentTabHost.TabSpec tab = tabHost.newTabSpec(tabTitle[i]).setIndicator(getTabView(i)); tabHost.addTab(tab, fragment[i], null); } } /** * 获取tabview * @param index * @return */ private View getTabView(int index) { View tabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null); ImageView tabImageView = (ImageView) tabView.findViewById(R.id.tab_image); tabImageView.setImageResource(R.drawable.line_selector); TextView tabTextView = (TextView) tabView.findViewById(R.id.tab_text); tabTextView.setText(tabTitle[index]); return tabView; }}
源码下载 http://download.csdn.net/detail/hu285279904/9655659
0 0
- android fragmenttabhost 使用之二
- android之FragmentTabHost使用
- android fragmenttabhost 使用之三
- Android常用控件之FragmentTabHost的使用
- Android常用控件之FragmentTabHost的使用
- Android学习之FragmentTabHost配合Fragment使用
- Android学习之--底部菜单FragmentTabHost使用
- Android之---FragmentTabHost使用简单介绍
- Android FragmentTabHost使用
- android FragmentTabHost的使用
- android fragmentTabHost的使用
- Android Studio FragmentTabHost使用
- Android控件使用之FragmentTabHost实现底部菜单
- Android自定义FragmentTabhost之ZzFragmentTabhost
- android FragmentTabHost 的简单使用
- android FragmentTabHost的简单使用
- android FragmentTabHost 的简单使用
- Android-FragmentTabHost的简单使用
- 常用的数据结构(二叉树)
- 计时器
- 顺序队列
- 利用辅助服务AccessibilityService实现微信自动抢红包
- 获取App和设备信息
- android fragmenttabhost 使用之二
- 构建高并发高可用的电商平台架构实践
- webstoem自动编译less文件
- cocos2d-JS 进阶主题 - JS到JAVA反射
- 获取txt pdf doc docx类型文件内容
- java 怎么获取jdk版本
- [Unity3D教程]Unity粒子系统Shuriken(飞镖)
- 如何实现android蓝牙开发 自动配对连接,并不弹出提示框
- QSetting:Qt读写配置文件实例