工具栏未显示在MainActivity前屏幕中.有人能帮我吗



我删除了操作栏并使用了工具栏,但它没有显示在我的应用程序中。我还在工具栏中使用了导航抽屉,但由于工具栏没有显示,所以我无法使用导航抽屉。当我向下滚动时,它会从上面显示一个小工具栏布局。所以我认为,我没有正确设置布局。

这是我的代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_height="match_parent"
android:id="@+id/drawer_layout"
android:fitsSystemWindows="true"
tools:context="edmt.dev.androidgridlayout.MainActivity"
tools:openDrawer="start">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/colorAccent"
android:id="@+id/toolbar"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/drawer_menu" />

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:orientation="vertical"
android:background="@drawable/bg"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="200dp">
<TextView
android:id="@+id/textGrid"
android:text="States of India"
android:textSize="34sp"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:onClick="callLoginActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LOGIN"
android:id="@+id/login"/>
</RelativeLayout>
<GridLayout
android:id="@+id/mainGrid"
android:columnCount="2"
android:rowCount="3"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false"
android:layout_weight="8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="14dp"
>
<!-- Row 1 -->
<!-- Column 1 -->
<androidx.cardview.widget.CardView
android:id="@+id/jnk"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="@drawable/jk"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="Jammu and Kashmir"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<!-- Column 2 -->
<androidx.cardview.widget.CardView
android:id="@+id/hp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
>
<LinearLayout
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="136dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/hpz" />
<TextView
android:text="Himachal Pradesh"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.cardview.widget.CardView>

</GridLayout>
</LinearLayout>
</ScrollView>
</androidx.drawerlayout.widget.DrawerLayout>

主要活动.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private CardView jnk,hp,up,mh,tn,rj,pb,kl,kr,hr,gj,ap,bhr,wb,goa,mp,tlg,odi,asm,cht,jkh,ngl,uk,skm,mnp,trp,arnp,mghl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//FCM ,Channel created....Supported after oreo update
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel channel= new NotificationChannel("MyNotifications","MyNotifications", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager=getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
//FCM ,Specify Topic Eg.(Weather)
FirebaseMessaging.getInstance().subscribeToTopic("Weather")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
String msg = "Successfull";
if (!task.isSuccessful()) {
msg = "Failed";
}
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
}
});


Toolbar toolbar=findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

drawer=findViewById(R.id.drawer_layout);
NavigationView navigationView=findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle=new ActionBarDrawerToggle(this, drawer,toolbar, R.string.navigation_drawer_open,R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();

jnk=(CardView)findViewById(R.id.jnk);
hp=(CardView)findViewById(R.id.hp);
up=(CardView)findViewById(R.id.up);
mh=(CardView)findViewById(R.id.mh);
tn=(CardView)findViewById(R.id.tn);
rj=(CardView)findViewById(R.id.rj);
pb=(CardView)findViewById(R.id.pb);
kl=(CardView)findViewById(R.id.kerala);
kr=(CardView)findViewById(R.id.krtk);
hr=(CardView)findViewById(R.id.hr);
gj=(CardView)findViewById(R.id.gj);
ap=(CardView)findViewById(R.id.ap);
bhr=(CardView)findViewById(R.id.bhr);
wb=(CardView)findViewById(R.id.wb);
goa=(CardView)findViewById(R.id.goa);
mp=(CardView)findViewById(R.id.mp);
tlg=(CardView)findViewById(R.id.tel);
odi=(CardView)findViewById(R.id.odi);
asm=(CardView)findViewById(R.id.assam);
cht=(CardView)findViewById(R.id.chhtt);
jkh=(CardView)findViewById(R.id.jhrk);
ngl=(CardView)findViewById(R.id.naga);
uk=(CardView)findViewById(R.id.uk);
skm=(CardView)findViewById(R.id.skm);
mnp=(CardView)findViewById(R.id.mani);
trp=(CardView)findViewById(R.id.tripura);
arnp=(CardView)findViewById(R.id.arunp);
mghl=(CardView)findViewById(R.id.megha);
jnk.setOnClickListener(this);
hp.setOnClickListener(this);
up.setOnClickListener(this);
mh.setOnClickListener(this);
tn.setOnClickListener(this);
rj.setOnClickListener(this);
pb.setOnClickListener(this);
kl.setOnClickListener(this);
kr.setOnClickListener(this);
hr.setOnClickListener(this);
gj.setOnClickListener(this);
ap.setOnClickListener(this);
bhr.setOnClickListener(this);
wb.setOnClickListener(this);
goa.setOnClickListener(this);
mp.setOnClickListener(this);
tlg.setOnClickListener(this);
odi.setOnClickListener(this);
asm.setOnClickListener(this);
cht.setOnClickListener(this);
jkh.setOnClickListener(this);
ngl.setOnClickListener(this);
uk.setOnClickListener(this);
skm.setOnClickListener(this);
mnp.setOnClickListener(this);
trp.setOnClickListener(this);
arnp.setOnClickListener(this);
mghl.setOnClickListener(this);
}
@Override
public void onBackPressed(){
if(drawer.isDrawerOpen(GravityCompat.START)){
drawer.closeDrawer(GravityCompat.START);
}else {
super.onBackPressed();
}
}

@Override
public void onClick(View view) {
Intent i;
switch (view.getId()){
case R.id.jnk : i=new Intent(this,jammu.class);startActivity(i);break;
case R.id.hp : i=new Intent(this,himachal.class);startActivity(i);break;
case R.id.up : i=new Intent(this,uttar.class);startActivity(i);break;
case R.id.mh : i=new Intent(this,maharashtra.class);startActivity(i);break;
case R.id.tn : i=new Intent(this,tamil.class);startActivity(i);break;
case R.id.rj : i=new Intent(this,rajasthan.class);startActivity(i);break;
case R.id.pb : i=new Intent(this,punjab.class);startActivity(i);break;
case R.id.kerala : i=new Intent(this,kerala.class);startActivity(i);break;
case R.id.krtk : i=new Intent(this,karnataka.class);startActivity(i);break;
case R.id.hr : i=new Intent(this,haryana.class);startActivity(i);break;
case R.id.gj : i=new Intent(this,gujarat.class);startActivity(i);break;
case R.id.ap : i=new Intent(this,andhra.class);startActivity(i);break;
case R.id.bhr : i=new Intent(this,bihar.class);startActivity(i);break;
case R.id.wb : i=new Intent(this,west.class);startActivity(i);break;
case R.id.goa : i=new Intent(this,goaz.class);startActivity(i);break;
case R.id.mp : i=new Intent(this,madhya.class);startActivity(i);break;
case R.id.tel : i=new Intent(this,telangana.class);startActivity(i);break;
case R.id.odi : i=new Intent(this,odisha.class);startActivity(i);break;
case R.id.assam : i=new Intent(this,assam.class);startActivity(i);break;
case R.id.chhtt : i=new Intent(this,chhattisgarh.class);startActivity(i);break;
case R.id.jhrk : i=new Intent(this,jharkhand.class);startActivity(i);break;
case R.id.naga : i=new Intent(this,nagaland.class);startActivity(i);break;
case R.id.uk : i=new Intent(this,uttarakhand.class);startActivity(i);break;
case R.id.skm : i=new Intent(this,sikkim.class);startActivity(i);break;
case R.id.mani : i=new Intent(this,manipur.class);startActivity(i);break;
case R.id.tripura : i=new Intent(this,tripura.class);startActivity(i);break;
case R.id.arunp : i=new Intent(this,arunachal.class);startActivity(i);break;
case R.id.megha : i=new Intent(this,meghalaya.class);startActivity(i);break;
default:break;
}
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.login:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new login_fragment()).commit();
break;
case R.id.share:
Toast.makeText(this,"Share",Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void callLoginActivity(View view){
Intent i = new Intent(getApplicationContext(), loginactivity.class);
startActivity(i);
}
}

样式.xml

<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="MyTheme" parent="AppTheme">
<item name="android:windowBackground">@android:color/white</item>
</style>
</resources>

AndroidManifest.xml

在清单中包括此代码行:

<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">

您的ScrollView隐藏Toolbar。尝试删除ScrollView或将ScrollView移动到Toolbar下方的LinearLayout。检查以下内容:

<androidx.drawerlayout.widget.DrawerLayout>
<LinearLayout>
<androidx.appcompat.widget.Toolbar/>
<ScrollView/>
<FrameLayout/>
</LinearLayout>
<com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>

试试这个:在Style.xml中,将Theme.AppCompat.Light.DarkActionBar更改为Theme.AppCompat.NoActionBar

最新更新