Вроде сделал menubar, но у меня ошибка....

This commit is contained in:
Максим 2023-10-26 16:32:39 +03:00
parent f7ca7b8057
commit 1ffd67fa14
9 changed files with 143 additions and 27 deletions

View File

@ -51,6 +51,9 @@ android {
dependencies {
implementation("androidx.navigation:navigation-compose:2.7.4")
implementation("androidx.compose.material:material:1.5.4")
implementation("androidx.core:core-ktx:1.9.0")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.0")
@ -58,7 +61,7 @@ dependencies {
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")
//implementation("androidx.compose.material3:material3")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

View File

@ -4,12 +4,10 @@ import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.example.labwork.button_navigation.MainScreen
import com.example.labwork.ui.theme.LabWorkTheme
class MainActivity : ComponentActivity() {
@ -17,30 +15,8 @@ class MainActivity : ComponentActivity() {
super.onCreate(savedInstanceState)
setContent {
LabWorkTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Android")
MainScreen()
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
LabWorkTheme {
Greeting("Android")
}
}

View File

@ -0,0 +1,21 @@
package com.example.labwork.button_navigation
import android.annotation.SuppressLint
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
import androidx.navigation.compose.rememberNavController
@SuppressLint("UnusedMaterialScaffoldPaddingParameter")
@Composable
fun MainScreen() {
val navController = rememberNavController()
Scaffold(
bottomBar = {
SlideNavigation(navController = navController)
}
) {
SlideGraph(navHostController = navController)
}
}

View File

@ -0,0 +1,23 @@
package com.example.labwork.button_navigation
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
@Composable
fun ScreenInfo() {
Text(
modifier = Modifier.fillMaxSize().wrapContentHeight(),
text = "Тут будет главный экран..."
)
}
@Composable
fun ScreenProfile() {
Text(
modifier = Modifier.fillMaxSize().wrapContentHeight(),
text = "Тут будет профиль экран..."
)
}

View File

@ -0,0 +1,22 @@
package com.example.labwork.button_navigation
import androidx.compose.runtime.Composable
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
@Composable
fun SlideGraph(
navHostController: NavHostController
) {
NavHost(navController = navHostController, startDestination = "Profile"){
composable("Profile"){
ScreenProfile()
}
composable("Info"){
ScreenInfo()
}
}
}

View File

@ -0,0 +1,8 @@
package com.example.labwork.button_navigation
import com.example.labwork.R
sealed class SlideItem(val title: String, val iconId: Int, val route: String){
object ScreenInfo: SlideItem("Информация", R.drawable.baseline_info,"Info" )
object ScreenProfile: SlideItem("Профиль", R.drawable.baseline_account_circle,"Profile" )
}

View File

@ -0,0 +1,53 @@
package com.example.labwork.button_navigation
import androidx.compose.material.BottomNavigation
import androidx.compose.material.BottomNavigationItem
import androidx.compose.material.Icon
import androidx.compose.material.SnackbarDefaults.backgroundColor
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import androidx.navigation.compose.currentBackStackEntryAsState
@Composable
fun SlideNavigation(
navController: NavController
) {
val listItems = listOf(
SlideItem.ScreenInfo,
SlideItem.ScreenProfile
)
BottomNavigation(
backgroundColor = Color.White
) {
val backStackEntry by navController.currentBackStackEntryAsState()
val currentRout = backStackEntry?.destination?.route
listItems.forEach { item ->
BottomNavigationItem(
selected = currentRout == item.route,
onClick = {
navController.navigate(item.route)
},
icon = {
Icon(
painter = painterResource(id = item.iconId),
contentDescription = "Icon"
)
},
label = {
Text(
text = item.title,
fontSize = 9.sp
)
},
selectedContentColor = Color.Blue,
unselectedContentColor = Color.Gray
)
}
}
}

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10s10,-4.48 10,-10S17.52,2 12,2zM12,6c1.93,0 3.5,1.57 3.5,3.5S13.93,13 12,13s-3.5,-1.57 -3.5,-3.5S10.07,6 12,6zM12,20c-2.03,0 -4.43,-0.82 -6.14,-2.88C7.55,15.8 9.68,15 12,15s4.45,0.8 6.14,2.12C16.43,19.18 14.03,20 12,20z"/>
</vector>

View File

@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM13,17h-2v-6h2v6zM13,9h-2L11,7h2v2z"/>
</vector>