安卓在fragment里面使用TabHost布局
来源:互联网 发布:淘宝冻结账户怎么解冻 编辑:程序博客网 时间:2024/05/17 09:08
效果图如下:
“投资”Fragment的xml布局如下:
<LinearLayout 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"
android:orientation="vertical"
tools:context=".Fragment_Money" >
<TextView
android:id="@+id/money_caption"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="0dp"
android:background="#FF3300"
android:gravity="center"
android:padding="0dp"
android:text="我要投资"
android:textColor="#ffffff" />
<ScrollView
android:id="@+id/scr1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TabWidget的id属性必须为 @android:id/tabs -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<!-- FrameLayout的id属性必须为 @android:id/tabcontent -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/investment_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/financing_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</ScrollView>
</LinearLayout>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragment_Money" >
<TextView
android:id="@+id/money_caption"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_margin="0dp"
android:background="#FF3300"
android:gravity="center"
android:padding="0dp"
android:text="我要投资"
android:textColor="#ffffff" />
<ScrollView
android:id="@+id/scr1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true" >
<TabHost
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- TabWidget的id属性必须为 @android:id/tabs -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
<!-- FrameLayout的id属性必须为 @android:id/tabcontent -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/investment_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="@+id/financing_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</ScrollView>
</LinearLayout>
1:在代码中初始化Tabhost
tabHost=(TabHost)getActivity().findViewById(R.id.tabhost);
tabHost.setup();
//创建Tab标签
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("项目直投",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.investment_list));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("众筹投资",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.financing_list));
tabHost.setup();
//创建Tab标签
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("项目直投",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.investment_list));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("众筹投资",
getResources().getDrawable(R.drawable.ic_launcher)).setContent(
R.id.financing_list));
注意:这段代码是在继承了Fragment的Fragment类中写的,所以“getActivity()”就相当于Activity类中的this。
2:代码中初始化TabHost中的View
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 20);
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.setMargins(0, 0, 0, 20);
investmentList=(LinearLayout)getView().findViewById(R.id.investment_list);//读取xml布局(容器)
for(int i=0;i<20;i++)
{
View v = inflater.inflate(R.layout.list_model, null);//实例xml转成View
v.setLayoutParams(lp);
v.setId(i);
investmentList. addView (v);
}
{
View v = inflater.inflate(R.layout.list_model, null);//实例xml转成View
v.setLayoutParams(lp);
v.setId(i);
investmentList. addView (v);
}
说明:我这里是把“R.id.investment_list”这个布局文件转成了View
R.id.investment_list 的xml布局代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_model"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:paddingLeft="20dp" >
<TextView
android:id="@+id/txtCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="某手机卖场短期借贷二期"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="金额:" />
<TextView
android:id="@+id/txtMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="50.00万元"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="期限" />
<TextView
android:id="@+id/txt_time_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="2月"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="利率:" />
<TextView
android:id="@+id/txt_interest_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11.50%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF3300" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:text="到期还本息" />
<View
android:layout_width="100dp"
android:layout_height="4px"
android:layout_gravity="center"
android:layout_marginRight="30dp"
android:background="#FF3300" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%" />
</LinearLayout>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_model"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:paddingLeft="20dp" >
<TextView
android:id="@+id/txtCaption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="某手机卖场短期借贷二期"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="金额:" />
<TextView
android:id="@+id/txtMoney"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="50.00万元"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="期限" />
<TextView
android:id="@+id/txt_time_limit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:text="2月"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="利率:" />
<TextView
android:id="@+id/txt_interest_rate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="11.50%"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FF3300" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:text="到期还本息" />
<View
android:layout_width="100dp"
android:layout_height="4px"
android:layout_gravity="center"
android:layout_marginRight="30dp"
android:background="#FF3300" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="100%" />
</LinearLayout>
</LinearLayout>
注意:不熟悉Fragment生命周期的朋友可以去查看相关文档,以上代码均写在 onViewCreated()的方法中,super.onViewCreated(view, savedInstanceState);语句之前。
细节:为了使tabhost中加入scrollview的滑动效果,需要把ScrollView标签放在TabHost便签的外层。
不懂的朋友欢迎大家给我留言
0 0
- 安卓在fragment里面使用TabHost布局
- 在TabHost里面屏蔽安卓物理按键
- 安卓布局——tabhost底部
- 在Fragment里面实现listView布局
- 安卓控件使用系列29:TabHost卡片的使用方法2不继承TabActivity、TabHost控件、一个布局文件
- 安卓控件使用系列30:TabHost卡片的使用方法3不继承TabActivity、TabHost控件、多个布局文件
- 安卓 更新 fragment 里面 控件
- TabHost+Fragment使用
- 使用Fragment替换TabHost
- 安卓布局 viewpager+tablelayout+fragment
- Fragment+TabHost模仿新浪新闻布局界面
- Fragment+TabHost模仿新浪新闻布局界面
- 【安卓笔记】ViewPager+Fragment布局中的Fragment数据更新
- 使用Fragment实现tabhost效果
- 使用Fragment实现tabhost效果
- 使用TabHost+Fragment实现导航
- 使用Fragment实现tabhost效果
- android Fragment TabHost 的使用
- IOS基础:retain,copy,assign及autorelease
- Nginx goaccess 安装应用指南
- 关于iOS多线程
- 测试 有没有
- InputStreamReader和BufferedReader用法及真实案例
- 安卓在fragment里面使用TabHost布局
- glFlush,glFinish和SwapBuffers用法
- OC学习----NSURL
- Ehcache 整合Spring 使用页面、对象缓存
- ZooKeeper-3.3.4集群安装配置
- kafka 简单介绍
- 媒体视频播放器应用源码
- android中写文件到内部存储中
- leetcode Course Schedule