我几个月来一直致力于这个项目,但在发布时我的主要布局中没有任何元素。
以下是屏幕截图https://www.dropbox.com/s/99d2lr6ffo4klxc/Screenshot_20180413-064226.png?dl=0
这是我的构建gradle文件。
buildscript {
repositories {
jcenter()
google()
mavenCentral()
maven {
url "https://maven.google.com"
}
maven { url 'https://maven.fabric.io/public' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath 'com.google.gms:google-services:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
google()
maven {
url "https://maven.google.com"
}
maven { url 'https://maven.fabric.io/public' }
maven { url "https://jitpack.io" }
mavenCentral()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
这是我的应用程序gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.cofoz.cofoz"
buildToolsVersion '27.0.3'
minSdkVersion 17
targetSdkVersion 27
versionCode 2
versionName "1.1"
vectorDrawables.useSupportLibrary = true
multiDexEnabled true
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"
}
useLibrary 'org.apache.http.legacy'
dexOptions {
javaMaxHeapSize "2g"
preDexLibraries true
jumboMode true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
// Disables PNG crunching for the release build type.
crunchPngs false
}
}
sourceSets { main { assets.srcDirs = ['src/main/assets',
'src/main/assets/'] } }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.0.2'
implementation 'com.android.support:support-v13:27.0.2'
implementation 'com.android.support:animated-vector-drawable:27.0.2'
implementation 'com.android.support:mediarouter-v7:27.0.2'
implementation 'com.android.support:support-compat:27.0.2'
implementation 'com.android.support:support-dynamic-animation:27.0.2'
implementation 'com.android.support:palette-v7:27.0.2'
implementation 'com.android.support:design:27.0.2'
implementation 'com.android.support:animated-vector-drawable:27.0.2'
implementation 'com.android.support:customtabs:27.0.2'
implementation 'com.android.support:cardview-v7:27.0.2'
implementation 'com.android.support:recyclerview-v7:27.0.2'
implementation 'com.android.support.constraint:constraint-
layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
implementation 'com.google.android.gms:play-services-auth:11.8.0'
implementation 'com.google.firebase:firebase-core:11.8.0'
implementation 'com.github.andremion:counterfab:1.0.1'
implementation 'com.firebaseui:firebase-ui-auth:3.2.2'
implementation 'com.google.firebase:firebase-auth:11.8.0'
//implementation 'com.jakewharton:butterknife:8.8.1'
implementation 'com.firebaseui:firebase-ui-firestore:3.2.2'
implementation 'com.google.firebase:firebase-firestore:11.8.0'
implementation 'com.google.firebase:firebase-messaging:11.8.0'
// Required only if Facebook login support is required
//implementation 'com.facebook.android:facebook-android-sdk:4.27.0'
implementation 'com.github.bumptech.glide:glide:4.4.0'
//implementation 'com.github.DanielMartinus:Stepper-Touch:0.6'
//implementation 'com.stone.vega.library:VegaLayoutManager:1.0.6'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.okhttp:okhttp:2.5.0'
implementation 'com.google.code.gson:gson:2.8.2'
//implementation'com.google.firebase:firebase-invites:11.8.0'
implementation 'de.hdodenhof:circleimageview:2.1.0'
implementation 'com.airbnb.android:lottie:2.2.5'
implementation 'com.google.android.gms:play-services:11.8.0'
implementation 'com.google.android.gms:play-services-maps:11.8.0'
implementation 'com.google.android.gms:play-services-location:11.8.0'
//implementation 'com.mcxiaoke.volley:library-aar:1.0.0'
implementation 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
//implementation 'com.github.GrenderG:Toasty:1.2.8'
implementation 'com.github.d-max:spots-dialog:0.4@aar'
implementation 'com.wdullaer:materialdatetimepicker:3.5.0'
//implementation 'com.github.ganfra:material-spinner:2.0.0'
//implementation 'com.google.android.gms:play-services-identity:11.8.0'
//implementation 'com.github.thomper:sweet-alert-dialog:v1.4.0'
// Shimmer
implementation 'com.facebook.shimmer:shimmer:0.1.0@aar'
implementation 'com.karumi:dexter:4.2.0'
implementation 'com.afollestad.material-dialogs:core:0.9.6.0'
}
apply plugin: 'com.google.gms.google-services'
我的mainactivity.java是这样的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Window window = getWindow();
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// finally change the color
window.setStatusBarColor(ContextCompat.getColor(MainActivity.this, R.color.colorPrimary));
}
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
LinearLayout bottomsheetLayout = (LinearLayout) findViewById(R.id.bottom_sheet);
counterFab = (CounterFab) findViewById(R.id.counter_fab);
bottomDishName = (TextView) findViewById(R.id.dishNameBottom);
dishBottomPrice = (TextView)findViewById(R.id.dishBottomPrice);
txtIngredients = (TextView) findViewById(R.id.txtIngredients);
counterFab.setTranslationY(counterFab.getHeight() + 16);
floatingLay = (CoordinatorLayout) findViewById(R.id.floatingLay);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
headerName = (TextView)findViewById(R.id.headerName);
mLogout = (LinearLayout)findViewById(R.id.logout);
mAuth = FirebaseAuth.getInstance();
mUser = mAuth.getCurrentUser();
user_id = mUser.getUid();
mFirestore = FirebaseFirestore.getInstance();
mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
mDatabaseUser = mFirestore.collection("Users").document(user_id);
bottomSheetBehavior = BottomSheetBehavior.from(bottomsheetLayout);
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
counterFab.animate().translationY(0).setInterpolator(new DecelerateInterpolator(2)).start();
}
}, 100);
cardLoading = (CardLoading) getApplicationContext();
counterFab.setCount(cardLoading.getFabCount());
//gps = new GPSTracker(MainActivity.this);
//Toast.makeText(this, "Location:"+Double.toString(gps.getLatitude()) + Double.toString(gps.getLongitude()), Toast.LENGTH_SHORT).show();
//Toast.makeText(this, "FabCount:"+Integer.toString(cardLoading.getFabCount()), Toast.LENGTH_SHORT).show();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
/*mFusedLocationClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
Toast.makeText(MainActivity.this, "Location:" + Double.toString(location.getLatitude()) + "," + Double.toString(location.getLongitude()), Toast.LENGTH_SHORT).show();
}
});*/
//Toast.makeText(this, userName, Toast.LENGTH_SHORT).show();
//Toast.makeText(this, userEmail, Toast.LENGTH_SHORT).show();
/*FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});*/
//locationStr = getIntent().getStringExtra("Location");
sp = getSharedPreferences("profile_data", MODE_PRIVATE);
spCart = getSharedPreferences("Cart_details", MODE_PRIVATE);
//String name = sp.getString("Name", "Your Name");
//String cartCount = spCart.getString("CartCount", "0");
//Toast.makeText(this, cartCount, Toast.LENGTH_SHORT).show();
//Integer c = Integer.valueOf(cartCount);
//counterFab.setCount(c);
View header=navigationView.getHeaderView(0);
View view=navigationView.inflateHeaderView(R.layout.nav_header_main);
headerName = (TextView)header.findViewById(R.id.headerName);
String userName = getIntent().getStringExtra("name");
mDatabaseUser.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(DocumentSnapshot documentSnapshot, FirebaseFirestoreException e) {
Users users = documentSnapshot.toObject(Users.class);
headerName.setText(users.getName());
}
});
//Toast.makeText(this, userName, Toast.LENGTH_SHORT).show();
//headerName.setText(userName);
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.beginFakeDrag();
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mLogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mAuth.signOut();
Intent logoutIntent = new Intent(MainActivity.this, WelcomeActivity.class);
startActivity(logoutIntent);
}
});
}
@Override
public void onResume()
{ // After a pause OR at startup
super.onResume();
//Refresh your stuff here
counterFab.setCount(cardLoading.getFabCount());
//Toast.makeText(this, "FabCount:"+Integer.toString(cardLoading.getFabCount()), Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
Fragment fragment = null;
int id = item.getItemId();
switch (id){
case R.id.nav_profile:
Intent profileIntent = new Intent(MainActivity.this, Profile.class);
startActivity(profileIntent);
break;
case R.id.nav_orders:
Intent ordersIntent = new Intent(MainActivity.this, Orders.class);
startActivity(ordersIntent);
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
switch (position){
case 0:
OrderNow tabOrdernow = new OrderNow();
return tabOrdernow;
case 1:
MealPlan tabMealPlan = new MealPlan();
return tabMealPlan;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Order Now";
case 1:
return "Meal Plan";
}
return null;
}
}
}
这是我的mainactivity.xml文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/colorPrimary"
app:menu="@menu/activity_main_drawer">
<LinearLayout
android:id="@+id/logout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="30dp"
android:clickable="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.2"
android:padding="5dp"
app:srcCompat="@drawable/ic_power_settings_new_black_24dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_vertical"
android:padding="5dp"
android:text="Log Out"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
</LinearLayout>
</android.support.design.widget.NavigationView>
这是我的app_bar_main文件
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/floatingLay"
android:layout_height="match_parent"
android:background="@android:color/white"
tools:context="com.cofoz.cofoz.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/colorPrimary"
android:elevation="5dp"
app:popupTheme="@style/AppTheme.PopupOverlay">
<ImageView
android:layout_width="69dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="logo"
android:src="@mipmap/white_text_cofoz" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorPrimary"
app:tabPadding="10dp"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@android:color/black" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<include layout="@layout/content_main" />
<include
layout="@layout/bottom_sheet"
/>
<com.andremion.counterfab.CounterFab
android:id="@+id/counter_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="@color/colorPrimary"
app:borderWidth="0dp"
app:elevation="11dp"
app:srcCompat="@drawable/ic_shopping_cart_black_24dp" />
答案 0 :(得分:1)
在MainActivity中添加此内容
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
然后我建议您移除navigationView
上的孩子并在activity_main_drawer
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:itemIconTint="@color/colorPrimary"
app:itemTextColor="@color/colorPrimary"
app:menu="@menu/activity_main_drawer" /> //This should end here
//activity drawer menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="@+id/logout"
android:icon="@drawable/logout_icon"
android:title="Logout" />
</group>
然后在mainActivity中处理点击事件工具OnNavigationItemSelectedListener
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.logout) {
//Do something
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
希望这有帮助
编辑:
如果您真的想在navigationView
中添加自定义视图,则需要创建该视图的实例,然后添加onclicklistener