我是Android开发的新手。我正在学习UI设计。我想要一个解决方案,其中有一个底部栏,其中5个选项直接链接到5个不同的活动。我从其他堆栈溢出答案中获得了Java解决方案(How to change activity on bottom navigation button click?) - 第二个答案是sushil,但它没有活动 - XML文件可供我理解。
像这样的底栏:
基于底栏加载的活动:
答案 0 :(得分:0)
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:
Intent intent1 = new Intent(this, AnActivity.class);
startActivity(intent1);
break;
case R.id.action_schedules:
Intent intent2 = new Intent(this, AnotherActivity.class);
startActivity(intent2);
break;
case R.id.action_music:
Intent intent3 = new Intent(this, AnotherActivity.class);
startActivity(intent3);
break;
}
return true;
}
});
答案 1 :(得分:0)
根据附件中的图片,您应该使用Fragment
代替Activity
5个不同的MenuItem
或选项来实现所需的输出。
1。为5个不同的Fragments
创建5个不同的MenuItem
。
例如:MatchingFragment
,WatchListFragment
,RatesFragment
,DealsFragment
和ListingFragment
。
2。将OnNavigationItemSelectedListener
添加到NavigationView
,然后根据您选择的Fragment
更改MenuItem
。使用以下代码更改Fragment
:
// Set action to perform when any menu-item is selected.
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Change Fragment
selectFragment(item);
return false;
}
});
/**
* Perform action when any item is selected.
*
* @param item Item that is selected.
*/
protected void selectFragment(MenuItem item) {
item.setChecked(true);
switch (item.getItemId()) {
case R.id.action_matching:
// Action to perform when Matching Menu item is selected.
pushFragment(new MatchingFragment());
break;
case R.id.action_watch_list:
// Action to perform when WatchList Menu item is selected.
pushFragment(new WatchListFragment());
break;
case R.id.action_rates:
// Action to perform when Rates Menu item is selected.
pushFragment(new RatesFragment());
break;
case R.id.action_deals:
// Action to perform when Deals Menu item is selected.
pushFragment(new DealsFragment());
break;
case R.id.action_listing:
// Action to perform when Listing Menu item is selected.
pushFragment(new ListingFragment());
break;
}
}
/**
* Method to push any fragment into given id.
*
* @param fragment An instance of Fragment to show into the given id.
*/
protected void pushFragment(Fragment fragment) {
if (fragment == null)
return;
FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
FragmentTransaction ft = fragmentManager.beginTransaction();
if (ft != null) {
ft.replace(R.id.rootLayout, fragment);
ft.commit();
}
}
}
以下是完整的教程:Android Bottom Navigation View Tutorial With Example
希望这会有所帮助〜
答案 2 :(得分:0)
基本活动:
public abstract class BaseActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {
protected BottomNavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewId());
navigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
navigationView.setOnNavigationItemSelectedListener(this);
}
@Override
protected void onStart() {
super.onStart();
updateNavigationBarState();
}
// Remove inter-activity transition to avoid screen tossing on tapping bottom navigation items
@Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navigationView.postDelayed(() -> {
int itemId = item.getItemId();
if (itemId == R.id.navigation_analysis) {
startActivity(new Intent(getApplicationContext(), AnalysisActivity.class));
} else if (itemId == R.id.navigation_dashboard) {
startActivity(new Intent(getApplicationContext(), DashboardActivity.class));
} else if (itemId == R.id.navigation_profile) {
startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
}
finish();
}, 100);
return true;
}
private void updateNavigationBarState(){
int actionId = getNavigationMenuItemId();
selectBottomNavigationBarItem(actionId);
}
void selectBottomNavigationBarItem(int itemId) {
Menu menu = navigationView.getMenu();
for (int i = 0, size = menu.size(); i < size; i++) {
MenuItem item = menu.getItem(i);
boolean shouldBeChecked = item.getItemId() == itemId;
if (shouldBeChecked) {
item.setChecked(true);
break;
}
}
}
abstract int getContentViewId();
abstract int getNavigationMenuItemId();
}
确保在onNavigationSelected()中使用getApplicationContext。
public class DashboardActivity extends BaseActivity {
@Override
int getContentViewId() {
return R.layout.activity_dashboard ;
}
@Override
int getNavigationMenuItemId() {
return R.id.navigation_dashboard;
} }
类似地-像这样进行其他活动。
XML PART
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"
/>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_analysis"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_analysis" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard" />
<item
android:id="@+id/navigation_profile"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_profile" />
</menu>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="@+id/frame_dashboard">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/title_dashboard"/>
</FrameLayout>
<include
layout="@layout/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
/>
</LinearLayout>
类似地-像这样制作其他xml文件。 希望对您有帮助