android 水平滚动tab,FragmentTabHost水平滚动型+刷卡的Android

该博客详细介绍了如何在Android中使用FragmentTabHost创建带有多个子Fragment的TabLayout,展示了如何设置Tab标签、传递参数,并解决了可能的布局问题。作者还探讨了如何将LinearLayout嵌入HorizontalScrollView以实现卡片式布局的滚动效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我片段的onCreateView方法

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_home, container, false);

tabHost = (FragmentTabHost) rootView.findViewById(R.id.tabhost);

tabHost.setup(getActivity(),getFragmentManager()/* getChildFragmentManager()*/, R.layout.fragment_home);

Bundle arg1 = new Bundle();

arg1.putInt("Arg for Frag1", 1);

tabHost.addTab(tabHost.newTabSpec("Tab1").setIndicator("Latest Videos"), HomeFragmentChild1.class, arg1);

Bundle arg2 = new Bundle();

arg2.putInt("Arg for Frag2", 2);

tabHost.addTab(tabHost.newTabSpec("Tab2").setIndicator("Hot Videos"), HomeFragmentChild2.class, arg2);

Bundle arg3 = new Bundle();

arg3.putInt("Arg for Frag2", 3);

tabHost.addTab(tabHost.newTabSpec("Tab3").setIndicator("My Videos"), HomeFragmentChild2.class, arg3);

Bundle arg4 = new Bundle();

arg4.putInt("Arg for Frag2", 4);

tabHost.addTab(tabHost.newTabSpec("Tab4").setIndicator("Videos"), HomeFragmentChild2.class, arg4);

Bundle arg5 = new Bundle();

arg5.putInt("Arg for Frag2", 5);

tabHost.addTab(tabHost.newTabSpec("Tab5").setIndicator("Sample Videos"), HomeFragmentChild2.class, arg5);

TabWidget tw = (TabWidget) rootView.findViewById(R.id.tabs);

LinearLayout ll = (LinearLayout) tw.getParent();

HorizontalScrollView hs = new HorizontalScrollView(this.getActivity());

hs.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT));

ll.addView(hs, 0);

ll.removeView(tw);

hs.addView(tw);

hs.setHorizontalScrollBarEnabled(false);

return tabHost;

}

XML:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/tabhost"

android:layout_width="match_parent"

android:layout_height="match_parent" >

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/tabs"

android:layout_width="match_parent"a

android:layout_height="wrap_content"

android:orientation="horizontal" />

android:id="@android:id/tabcontent"

android:layout_width="0dp"

android:layout_height="0dp"

android:layout_weight="0" />

android:id="@+id/realtabcontent"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_weight="1" />

此外,才有可能实现方式刷卡和更改标签? 可能的错误在我的结尾?会有很大的帮助!谢谢!

+0

将您的LinearLayout到Horizo​​ntalScrollView? –

+0

不,它没有帮助:( –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值