我正在添加材质抽屉Material Drawer并制作了方法addNavigationDrawer以添加导航抽屉,并在从Firebase Firestore数据库获取用户详细信息后调用该方法(您可以在Auth Listener中的OnCreate方法下找到它),但是在第一次打开时您可以在该活动中切割该活动栏,如您在图像中看到的那样,但是当我将其最小化并再次打开时,它的工作效果就非常好。请建议该怎么办?
这是活动的完整代码
public class Main2Activity extends AppCompatActivity {
ProfileInformationDialog profileInformationDialog;
FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener;
FirebaseFirestore db;
DocumentReference documentReference;
Button button;
GoogleApiClient googleApiClient;
/*@BindView(R.id.toolbar)
Toolbar toolbar;*/
ProgressDialog progressDialog;
String user_name,shop_name;
Toolbar toolbar;
Drawer result;
AccountHeader headerResult;
@Override
public void onStart()
{
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
googleApiClient.connect();
super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("Hello");
setSupportActionBar(toolbar);
//DrawerUtil.getDrawer(Main2Activity.this,toolbar);
//addNavigationDrawer();
progressDialog = new ProgressDialog(this);
db = FirebaseFirestore.getInstance();
profileInformationDialog = new ProfileInformationDialog(this);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
ButterKnife.bind(this);
button = (Button) findViewById(R.id.log_out);
firebaseAuth = FirebaseAuth.getInstance();
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setCancelable(false);
//User is signed in
documentReference = db.collection("Users").document(firebaseUser.getUid());
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot)
{
if (documentSnapshot != null && documentSnapshot.exists()) {
progressDialog.hide();
User user1 = documentSnapshot.toObject(User.class);
user_name = user1.getName();
shop_name = user1.getShop_name();
addNavigationDrawer();
//DrawerUtil.getDrawer(Main2Activity.this,toolbar);
headerResult.updateProfile(new ProfileDrawerItem().withIcon(R.drawable.logout_icon256).withName(user_name));
Toast.makeText(Main2Activity.this, "Document Exists", Toast.LENGTH_SHORT).show();
}
else
{
progressDialog.hide();
Toast.makeText(Main2Activity.this, "No Such Document", Toast.LENGTH_SHORT).show();
profileInformationDialog.show();
profileInformationDialog.setCancelable(false);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
progressDialog.hide();
Toast.makeText(Main2Activity.this, "Exception " + e, Toast.LENGTH_SHORT).show();
}
});
Log.d("TAG", "onAuthStateChanged:signed_in:" + firebaseUser.getUid());
} else {
// User is signed out
Log.d("TAG", "onAuthStateChanged:signed_out");
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status)
{
//Toast.makeText(getApplicationContext(),"Google Logged Out",Toast.LENGTH_SHORT).show();
}
});
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(Main2Activity.this,SignInActivity.class));
finish();
}
});
}
private void addNavigationDrawer()
{
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.curve_shape)
.withSelectionListEnabledForSingleProfile(true)
.addProfiles(
new ProfileDrawerItem().withName(user_name).withIcon(R.drawable.logout_icon256)
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile)
{
Toast.makeText(Main2Activity.this, profile.getName() + "", Toast.LENGTH_SHORT).show();
return false;
}
})
.build();
PrimaryDrawerItem makeBillItem = new PrimaryDrawerItem().withIdentifier(1)
.withName("Make new Bill");
SecondaryDrawerItem logoutItem = new SecondaryDrawerItem().withIdentifier(2)
.withName("Log Out").withIcon(R.drawable.logout_icon256);
result = new DrawerBuilder()
.withAccountHeader(headerResult)
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentStatusBar(false)
.withDisplayBelowStatusBar(false)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.withCloseOnClick(true)
.withSelectedItem(-1)
.addDrawerItems(
makeBillItem,
new DividerDrawerItem(),
logoutItem
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
Toast.makeText(Main2Activity.this, position + " = position", Toast.LENGTH_SHORT).show();
return true;
}
})
.build();
}
@Override
public void onStop() {
super.onStop();
if (firebaseAuth != null) {
firebaseAuth.removeAuthStateListener(authStateListener);
}
}
}
最佳答案
步骤1将此样式添加到值/样式
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
步骤2将此样式添加到value / style-v21
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
步骤3并更改您的布局
<?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:layout_width="match_parent"
android:id="@+id/drawer_layout"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<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:layout_height="match_parent"
tools:context="com.visky.railway.sec.ui.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
-----------here your layout content---------------
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<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:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
步骤4将主题添加到manifest.xml
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar"/>
关于android - 工具栏从顶部开始切割。如何显示完整的工具栏?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47069169/