Je souhaite utiliser la barre de navigation inférieure dans mon application Android existante, mais le problème est que tous les écrans sont en activité, est-il possible de charger des activités sans masquer la barre de navigation inférieure.
exemple:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<Android.support.design.widget.CoordinatorLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<Android.support.v4.widget.NestedScrollView
Android:id="@+id/myScrollingContent"
Android:layout_width="match_parent"
Android:layout_height="match_parent">
<!-- Your loooooong scrolling content here. -->
</Android.support.v4.widget.NestedScrollView>
<com.roughike.bottombar.BottomBar
Android:id="@+id/bottomBar"
Android:layout_width="match_parent"
Android:layout_height="60dp"
Android:layout_gravity="bottom"
app:bb_tabXmlResource="@xml/bottom_bar"
app:bb_behavior="shy"/>
</Android.support.design.widget.CoordinatorLayout>
c'est mon activité de base,
MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomBar bottomBar;
bottomBar = (BottomBar) findViewById(R.id.bottomBar);
bottomBar.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelected(@IdRes int tabId) {
if (tabId == R.id.matching) {
Log.i("matching","matching inside "+tabId);
Intent in=new Intent(getBaseContext(),Main2Activity.class);
startActivity(in);
}else if (tabId == R.id.watchlist) {
Log.i("matching","watchlist inside "+tabId);
Intent in=new Intent(getBaseContext(),Main3Activity.class);
startActivity(in);
}
}
});
}
}
Main2Activity
public class Main2Activity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main2);
NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
View wizard = getLayoutInflater().inflate(R.layout.activity_main2, null);
dynamicContent.addView(wizard);
Main3Activity
public class Main3Activity extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main3);
NestedScrollView dynamicContent = (NestedScrollView) findViewById(R.id.myScrollingContent);
View wizard = getLayoutInflater().inflate(R.layout.activity_main3, null);
dynamicContent.addView(wizard);
}
}
manifeste
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:Android="http://schemas.Android.com/apk/res/Android"
package="com.bottom.bottomnavigation">
<application
Android:allowBackup="true"
Android:icon="@mipmap/ic_launcher"
Android:label="@string/app_name"
Android:supportsRtl="true"
Android:theme="@style/AppTheme">
<activity Android:name=".Main2Activity">
<intent-filter>
<action Android:name="Android.intent.action.MAIN" />
<category Android:name="Android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity Android:name=".MainActivity" />
<activity Android:name=".Main3Activity"></activity>
</application>
</manifest>
J'ai résolu ce problème de la manière suivante:
1. Créez un BaseActivity avec la barre de navigation inférieure.
package com.example.Apple.bottomnavbarwithactivity;
import Android.content.Intent;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.util.Log;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
public class BaseActivity extends AppCompatActivity {
RadioGroup radioGroup1;
RadioButton deals;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
radioGroup1=(RadioGroup)findViewById(R.id.radioGroup1);
deals = (RadioButton)findViewById(R.id.deals);
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
Intent in;
Log.i("matching", "matching inside1 bro" + checkedId);
switch (checkedId)
{
case R.id.matching:
Log.i("matching", "matching inside1 matching" + checkedId);
in=new Intent(getBaseContext(),MatchingActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.watchList:
Log.i("matching", "matching inside1 watchlistAdapter" + checkedId);
in = new Intent(getBaseContext(), WatchlistActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.rates:
Log.i("matching", "matching inside1 rate" + checkedId);
in = new Intent(getBaseContext(),RatesActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.listing:
Log.i("matching", "matching inside1 listing" + checkedId);
in = new Intent(getBaseContext(), ListingActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
case R.id.deals:
Log.i("matching", "matching inside1 deals" + checkedId);
in = new Intent(getBaseContext(), DealsActivity.class);
startActivity(in);
overridePendingTransition(0, 0);
break;
default:
break;
}
}
});
}
}
Présentation de BaseActivity nommée base_activity.xml
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="56dp"
Android:elevation="10dp"
Android:background="@color/white"
Android:id="@+id/bottonNavBar"
Android:paddingTop="5dp"
Android:orientation="horizontal"
Android:layout_alignParentBottom="true"
Android:foregroundGravity="bottom">
<RadioGroup
Android:id="@+id/radioGroup1"
Android:gravity="center"
Android:layout_height="match_parent"
Android:layout_width="match_parent"
Android:orientation="horizontal"
Android:baselineAligned="false">
<RadioButton
Android:layout_width="match_parent"
Android:gravity="center"
Android:layout_height="match_parent"
Android:text="Matching"
Android:layout_weight="1"
Android:button="@null"
Android:padding="2dp"
Android:checked="false"
Android:textSize="12sp"
Android:drawableTop="@drawable/selector_matching"
Android:textColor="@drawable/selector_nav_text"
Android:id="@+id/matching"/>
<RadioButton
Android:layout_width="match_parent"
Android:gravity="center"
Android:layout_height="match_parent"
Android:button="@null"
Android:layout_weight="1"
Android:padding="2dp"
Android:checked="false"
Android:textSize="12sp"
Android:drawableTop="@drawable/selector_watchlist"
Android:textColor="@drawable/selector_nav_text"
Android:id="@+id/watchList"
Android:text="Watchlist"/>
<RadioButton
Android:layout_width="match_parent"
Android:gravity="center"
Android:layout_height="match_parent"
Android:id="@+id/rates"
Android:button="@null"
Android:paddingTop="5dp"
Android:paddingBottom="2dp"
Android:paddingLeft="2dp"
Android:paddingRight="2dp"
Android:layout_weight="1"
Android:checked="false"
Android:textSize="12sp"
Android:drawableTop="@drawable/selector_rates"
Android:textColor="@drawable/selector_nav_text"
Android:text="Rates"/>
<RadioButton
Android:layout_width="match_parent"
Android:gravity="center"
Android:layout_height="match_parent"
Android:button="@null"
Android:padding="2dp"
Android:checked="false"
Android:layout_weight="1"
Android:textSize="12sp"
Android:drawableTop="@drawable/selector_deals"
Android:textColor="@drawable/selector_nav_text"
Android:id="@+id/deals"
Android:text="Deals"/>
<RadioButton
Android:layout_width="match_parent"
Android:gravity="center"
Android:layout_height="match_parent"
Android:button="@null"
Android:padding="2dp"
Android:checked="false"
Android:layout_weight="1"
Android:textSize="12sp"
Android:drawableTop="@drawable/selector_listing"
Android:textColor="@drawable/selector_nav_text"
Android:id="@+id/listing"
Android:text="Listing"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:id="@+id/dynamicContent"
Android:orientation="vertical"
Android:layout_marginBottom="56dp"
Android:background="@color/white"
Android:layout_gravity="bottom">
</LinearLayout>
2.extend BaseActivity dans toute l'activité que vous souhaitez ouvrir lors d'un clic de navigation inférieur et que vous devez également gonfler les présentations d'activité. Par exemple, j'ai créé cinq exemples d'activité.
i] MatchingActivity.
package com.example.Apple.bottomnavbarwithactivity;
import Android.graphics.Color;
import Android.os.Bundle;
import Android.support.design.widget.FloatingActionButton;
import Android.support.design.widget.Snackbar;
import Android.support.v7.app.AppCompatActivity;
import Android.support.v7.widget.Toolbar;
import Android.view.View;
import Android.widget.LinearLayout;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
//extends our custom BaseActivity
public class MatchingActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_matching);
//dynamically include the current activity layout into baseActivity layout.now all the view of baseactivity is accessible in current activity.
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_matching, null);
dynamicContent.addView(wizard);
//get the reference of RadioGroup.
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.matching);
// Change the corresponding icon and text color on nav button click.
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_matching_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
ii] WatchlistActivity
package com.example.Apple.bottomnavbarwithactivity;
import Android.graphics.Color;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.LinearLayout;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
public class WatchlistActivity extends AppCompatActivity {
LinearLayout dynamicContent,bottonNavBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_watchlist1);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_watchlist1, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.watchList);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.favourite_heart_selected, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
iii] RatesActivity
package com.example.Apple.bottomnavbarwithactivity;
import Android.graphics.Color;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.LinearLayout;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
public class RatesActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_rates);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_rates, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.rates);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_rate_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
iv] ListingActivity
package com.example.Apple.bottomnavbarwithactivity;
import Android.graphics.Color;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.LinearLayout;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
public class ListingActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_listing);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_listing, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.listing);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_listing_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
v] DealsActivity
package com.example.Apple.bottomnavbarwithactivity;
import Android.graphics.Color;
import Android.support.v7.app.AppCompatActivity;
import Android.os.Bundle;
import Android.view.View;
import Android.widget.LinearLayout;
import Android.widget.RadioButton;
import Android.widget.RadioGroup;
public class DealsActivity extends BaseActivity {
LinearLayout dynamicContent,bottonNavBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_deals);
dynamicContent = (LinearLayout) findViewById(R.id.dynamicContent);
bottonNavBar= (LinearLayout) findViewById(R.id.bottonNavBar);
View wizard = getLayoutInflater().inflate(R.layout.activity_deals, null);
dynamicContent.addView(wizard);
RadioGroup rg=(RadioGroup)findViewById(R.id.radioGroup1);
RadioButton rb=(RadioButton)findViewById(R.id.deals);
rb.setCompoundDrawablesWithIntrinsicBounds( 0,R.drawable.ic_deals_clicked, 0,0);
rb.setTextColor(Color.parseColor("#3F51B5"));
}
}
Remarque: assurez-vous de bien gérer la presse arrière correctement. Je n'ai pas écrit aucun code pour en arrière appuyez sur .
Avec Activity
, vous devez déclarer et initier que BottomBar
à chaque fois que vous chargez ce activity
.
Avec votre problème, ma réponse est [~ # ~] non [~ # ~] .
Btw, vous pouvez utiliser Fragment qui vous aide à résoudre ce très bon problème.
Il est temps d'apprendre quelque chose de nouveau, mon frère.
[~ # ~] éditer [~ # ~]
Vous apportez Fragment
uniquement à l'intérieur 1 Activité. Et laissez BottomBar
à l'intérieur de Activity
tandis que tous les autres affichent un ensemble de données à l'intérieur de Fragment
.
Juste l'essayer!