Панель инструментов не отображается на главном экране MainActivity. Может кто-нибудь помочь мне? - PullRequest
0 голосов
/ 07 ноября 2019

Я удалил панель действий и вместо нее использую панель инструментов, но она не отображается в моем приложении. Я также использовал навигационный ящик на своей панели инструментов, но так как моя панель инструментов не отображается, я не могу использовать навигационный ящик. когда я прокручиваю вниз, это показывает мне немного макет панели инструментов сверху. Так что я думаю, что я не правильно настроил макет.

Вот мой код:

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>

MainActivity.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);
}

}

Styles.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">

Ответы [ 2 ]

0 голосов
/ 07 ноября 2019

Ваш ScrollView скрывает Toolbar. Попробуйте удалить ScrollView или переместить ScrollView на LinearLayout ниже Toolbar. Проверьте ниже:

<androidx.drawerlayout.widget.DrawerLayout>
    <LinearLayout>
        <androidx.appcompat.widget.Toolbar/>
    <ScrollView/>
    <FrameLayout/>
    </LinearLayout>

    <com.google.android.material.navigation.NavigationView/>
</androidx.drawerlayout.widget.DrawerLayout>
0 голосов
/ 07 ноября 2019

Попробуйте: в Style.xml измените Theme.AppCompat.Light.DarkActionBar на Theme.AppCompat.NoActionBar.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...