我想TopAppBar向我的 Compose 应用程序添加一个,所以我执行了以下操作:
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AlternoTubeTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Scaffold(
topBar = {
TopAppBar(
title = {
Text(
stringResource(id = R.string.app_name),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
)
},
content = { innerPadding ->
MyAppTheme(modifier = Modifier.padding(innerPadding))
}
)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,当我运行应用程序时,我的TopAppBar:
而在预览图像上,应用栏确实有颜色:
接下来我可以尝试什么来获得正确的颜色?
android kotlin android-jetpack-compose android-jetpack-compose-material3
大约一个月前,Android 团队弃用了onCreateOptionsMenu和onOptionsItemSelected,以及setHasOptionsItemMenu. 不幸的是,这破坏了我的所有代码。
我的应用程序有很多片段,当用户导航到它们时,我总是确保菜单项会消失并在返回时重新出现,代码如下:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true)
}
Run Code Online (Sandbox Code Playgroud)
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
menu.clear()
}
Run Code Online (Sandbox Code Playgroud)
这段代码运行良好并且非常简单。现在 Android 团队已弃用(为什么?)setHasOptionsMenu,我无法重新创建此代码。
我了解用于扩充菜单项和处理菜单项单击事件的新语法,尽管我一生都无法弄清楚如何将菜单隐藏在片段中,然后使用新菜单在导航返回时再次显示它提供商 API。
这是我尝试过的:
导航到片段:
if (supportFragmentManager.backStackEntryCount == 0) {
supportFragmentManager.commit {
replace(R.id.activityMain_primaryFragmentHost, NewProjectFragment.newInstance(mainSpotlight != null))
addToBackStack(null)
}
}
Run Code Online (Sandbox Code Playgroud)
getRootMenuProvider接口中的函数ActivityFragment:
interface ActivityFragment {
val title: String
companion object {
fun getRootMenuProvider() = object : MenuProvider {
override fun onPrepareMenu(menu: Menu) {
for (_menuItem in menu.children) {
_menuItem.isVisible …Run Code Online (Sandbox Code Playgroud) 我正在 Compose 中创建一个应用程序,带有一个非常简单的导航栏,其中显示两个项目:主页和加星标:
@Composable
fun HomeScreen(modifier: Modifier) {
var selectedItem by remember { mutableStateOf(0) }
val items = listOf(
stringResource(id = R.string.bottomNavigationMenu_home),
stringResource(id = R.string.bottomNavigationMenu_starred)
)
val icons = listOf(
Icons.Outlined.Home,
Icons.Outlined.Star,
)
Box(
modifier = modifier
) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Bottom
) {
NewProjectButton(
modifier = Modifier
.padding(16.dp)
.align(Alignment.End)
)
NavigationBar {
items.forEachIndexed { index, item ->
NavigationBarItem(
icon = {
Icon(icons[index], contentDescription = item) },
label = {
Text(item)
},
selected = selectedItem == …Run Code Online (Sandbox Code Playgroud) 我正在为Android创建一个像素艺术编辑器,对于所有像素艺术编辑器来说,油漆桶(填充工具)是必须需要的。
为此,我在网上对洪水填充算法进行了一些研究。
我偶然发现了以下视频,其中解释了如何在代码中实现迭代洪水填充算法。视频中使用的代码是 JavaScript,但我可以轻松地将代码从视频转换为 Kotlin:
https://www.youtube.com/watch?v=5Bochyn8MMI&t=72s&ab_channel=crayoncode
以下是视频中 JavaScript 代码的摘录:
转换后的代码:
Tools.FILL_TOOL -> {
val seedColor = instance.rectangles[rectTapped]?.color ?: Color.WHITE
val queue = LinkedList<XYPosition>()
queue.offer(MathExtensions.convertIndexToXYPosition(rectangleData.indexOf(rectTapped), instance.spanCount.toInt()))
val selectedColor = getSelectedColor()
while (queue.isNotEmpty() && seedColor != selectedColor) { // While the queue is not empty the code below will run
val current = queue.poll()
val color = instance.rectangles.toList()[convertXYDataToIndex(instance, current)].second?.color ?: Color.WHITE
if (color != seedColor) {
continue
}
instance.extraCanvas.apply {
instance.rectangles[rectangleData[convertXYDataToIndex(instance, current)]] = defaultRectPaint // Colors in pixel with defaultRectPaint
drawRect(rectangleData[convertXYDataToIndex(instance, current)], …Run Code Online (Sandbox Code Playgroud) 今天打开 Android Studio 时,我感到惊讶 - 我看不到布局的预览:
我认为这只是一个简单的修复:
尽管这些都没有解决问题。
我也遇到过其他人遇到的渲染问题,并尝试了各种解决方案,尽管我仍然遇到问题。
奇怪的是,每当我TextInputLayout从布局中删除小部件时,渲染问题就会消失:
(以下是导致问题的布局之一。)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/fragmentNewCanvas_rootLayout"
tools:context=".fragments.newcanvas.NewCanvasFragment">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/fragmentNewCanvas_projectTitleTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:hint="@string/fragmentNewCanvas_project_name_str"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/fragmentNewCanvas_projectTitleTextInputEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/fragmentNewCanvas_spanCountTextInputLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="24dp"
android:hint="@string/fragmentNewCanvas_span_count_str"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/fragmentNewCanvas_projectTitleTextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/fragmentNewCanvas_spanCountTextInputEditText"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="@+id/fragmentNewCanvas_doneButton"
style="?attr/materialButtonOutlinedStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginBottom="24dp"
android:text="@string/str_button_done"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是演示问题的 gif:
不包含TextInputLayout渲染的布局很好,但包含渲染的布局会引起问题。
堆栈跟踪: …
最近,我决定更新我的 Espresso 代码以使用ActivityScenarioRule已弃用的ActivityTestRule.
为此,我将以下库导入到我的项目中:
最终代码使用ActivityScenarioRule:
package com.realtomjoney.pyxlmoose.activities.main
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.action.ViewActions
import androidx.test.espresso.matcher.ViewMatchers.*
import com.realtomjoney.pyxlmoose.R
import org.junit.Rule
import org.junit.Test
import androidx.test.espresso.action.ViewActions.click
import androidx.test.espresso.assertion.ViewAssertions
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.LargeTest
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import org.junit.runner.RunWith
@LargeTest
@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTestsWhenNewProjectIsCreated {
@get:Rule
var activityTestRule = ActivityScenarioRule(MainActivity::class.java)
private fun createNewProject() {
onView(withId(R.id.floatingActionButton)).perform(click())
onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputEditText)).perform(ViewActions.replaceText("Untitled Project"))
onView(withId(R.id.fragmentNewCanvas_spanCountTextInputEditText)).perform(ViewActions.replaceText("5"))
onView(withId(R.id.fragmentNewCanvas_doneButton)).perform(click())
}
@Test
fun uitest_fragmentNewCanvasProjectTitleTextInputEditText_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
createNewProject()
onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputEditText)).check(ViewAssertions.doesNotExist())
}
@Test
fun uitest_fragmentNewCanvasProjectTitleTextInputLayout_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
createNewProject()
onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputLayout)).check(ViewAssertions.doesNotExist())
}
@Test
fun uitest_fragmentNewCanvasSpanCountTextInputEditText_isNotDisplayed_after_fragmentNewCanvasDoneButton_isPressed() {
createNewProject()
onView(withId(R.id.fragmentNewCanvas_spanCountTextInputEditText)).check(ViewAssertions.doesNotExist())
}
@Test
fun …Run Code Online (Sandbox Code Playgroud) android ×6
kotlin ×4
algorithm ×1
android-jetpack-compose-material3 ×1
android-menu ×1
flood-fill ×1
java ×1
junit ×1