我正在使用实现com.roughike:bottom-bar:2.3.1
来创建一个bottom_bar,我想在底部栏项目上调用setOnTabSelectListener
,但是我无法从xml文件中找到带有R.id的底部栏项目。我怎么了?
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto">
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/bottom_bar"
>
</FrameLayout>
<com.roughike.bottombar.BottomBar
android:id="@+id/bottom_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/margin_50dp"
android:layout_alignParentBottom="true"
app:bb_tabXmlResource="@xml/tab"
>
</com.roughike.bottombar.BottomBar>
</RelativeLayout>
enter code here
<?xml version="1.0" encoding="utf-8"?>
<tabs>
<tab
id="@+id/home_tab"
icon="@drawable/home_btn"
/>
<tab
id="@+id/search_tab"
icon="@drawable/search_btn"
/>
<tab
id="@+id/new_post_tab"
icon="@drawable/new_post_btn"
/>
<tab
id="@+id/profile_tab"
icon="@drawable/profile_btn"
/>
</tabs>
private void init_bottomBar(){
bottomBar = findViewById(R.id.bottom_bar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(int tabId) {
switch (tabId)
{
case R.id.home_tab: <--here i cant find home_tab-->
break;
}
}
});
}
答案 0 :(得分:1)
如果您的ID来自另一个xml文件,则无法在“活动”中找到您的ID
尝试此方法:
View inflatedView = getLayoutInflater().inflate(R.layout.your_layout, null);
BottomBar bottomBar = (BottomBar) inflatedView.findViewById(R.id.bottom_bar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(int tabId) {
switch (tabId)
{
case R.id.home_tab:
break;
}
}
});
答案 1 :(得分:-1)