我在tablayout中有几个选项卡。XML:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:tabMode="scrollable">
<android.support.design.widget.TabItem
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="@+id/info_tab"
android:icon="@drawable/info"
android:clickable="true"/>
...
,我对如何为这些设置一个onclicklistener感到非常困惑。所有在线资源都假设我正在使用TABHOST,但是这些功能不适用于TableAut。我应该使用TABHOST吗?或tablayout上的onclicklistener会是什么样?
谢谢!
您可以使用此...
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
,在Ontabselected中,您可以编写代码以使用户单击该标签名称。
using MyTabLayout = Android.Support.Design.Widget.TabLayout;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
var tabLayout = FindViewById<MyTabLayout>(Resource.Id.sliding_tabs);
tabLayout.AddTab(tabLayout.NewTab().SetText("CNN"));
tabLayout.AddTab(tabLayout.NewTab().SetText("BBC"));
tabLayout.AddTab(tabLayout.NewTab().SetText("ABC7News"));
tabLayout.TabGravity = MyTabLayout.GravityFill;
tabLayout.TabSelected += TabLayout_TabSelected;
}
private void TabLayout_TabSelected(object sender, MyTabLayout.TabSelectedEventArgs e)
{
switch (e.Tab.Position)
{
case 0: ShowAlert("0"); break;
case 1: ShowAlert("1"); break;
case 2: ShowAlert("2"); break;
}
}
sliding_tabs - <android.support.design.widget.TabLayout />