我正在使用新的Android BottomNavigationView
,但底部导航MenuItem
动作视图未显示。
调试后,我发现菜单项不为空且可见性为可见。
检查根视图的高度和宽度后,即使在根元素的布局中硬编码值为layout_width=50dp
和layout_height=50dp
,它仍然为0。
这是我的底部导航菜单:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/nearby_fragment"
android:enabled="true"
android:icon="@drawable/homenearbyfragment"
android:title="@string/bottom_nearby"
app:showAsAction="always"/>
<item
android:id="@+id/route_fragment"
android:title="@string/bottom_homescreen"
android:enabled="true"
app:showAsAction="always"
android:icon="@drawable/home_mycommute" />
<item
android:id="@+id/newsfeed_activity"
android:title="@string/bottom_news"
android:enabled="true"
app:showAsAction="always"
android:icon="@drawable/newsfeed"
app:actionLayout="@layout/bagde_layout"/>
</menu>
我的操作布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="?android:attr/actionButtonStyle"
android:layout_width="50dp"
android:layout_height="50dp"
android:clickable="true"
android:focusable="true"
android:gravity="center">
<ImageView
android:id="@+id/menu_badge_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/newsfeed" />
<TextView
android:id="@+id/menu_badge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_marginTop="4dp"
android:background="@drawable/circle_bg"
android:gravity="center"
android:textColor="@android:color/white"
android:textSize="8sp"
/>
在我的Activity
中,我正在尝试设置:
private void setCountOnNews(Menu menu) {
mReportMenu = menu.findItem(R.id.newsfeed_activity);
FrameLayout count = (FrameLayout) mReportMenu.getActionView();
count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewsFeedActivity.class);
startActivity(intent);
MainApplication.getInstance().trackScreenView(getString(R.string.tracknewsfeedmenu));
}
});
TextView notifCount = (TextView) count.findViewById(R.id.menu_badge);
//if (Utils.getNewsfeedCountPreference(MainActivity.this) > 0)
notifCount.setText(String.valueOf(Utils.getNewsfeedCountPreference(MainActivity.this)));
notifCount.setText("asd;jfnapsdifnaspdifnasdpifnaspdfnapsdifnaspidfnapsidfnaspdiufif");
Log.d(TAG, "setCountOnNews:" + notifCount.getText().toString());
//else
// notifCount.setVisibility(View.GONE);
}