尝试使用TextInputField
Android 的新版本并想分享我的解决方案时发生了崩溃.
在android appcompat库中尝试新的TextInputField会导致我的应用程序崩溃.这是我的布局xml.
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="e-mail"
android:inputType="textEmailAddress"
android:singleLine="true"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
我得到的错误:
android.view.InflateException: Binary XML file line #20: Error inflating class android.support.design.widget.TextInputLayout.
Run Code Online (Sandbox Code Playgroud)
SOLUTION:将hintTextAppearance
属性添加到您的TextInputLayout
,因此lead标记如下所示:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hintTextAppearance="@android:style/TextAppearance.Medium">
Run Code Online (Sandbox Code Playgroud) 我正在创建一个问答,每个问题都是一张卡片.答案开始显示第一行,但是当点击它时,它应该展开以显示完整的答案.
当答案扩展/折叠时,RecyclerView的其余部分应设置动画以为扩展或折叠腾出空间,以避免显示空白区域.
我观看了有关RecyclerView动画的演讲,并且相信我想要一个自定义的ItemAnimator,我在其中覆盖animateChange.那时我应该创建一个ObjectAnimator来动画View的LayoutParams的高度.不幸的是,我很难将它们捆绑在一起.当覆盖canReuseUpdatedViewHolder时我也返回true,因此我们重用相同的viewholder.
@Override
public boolean canReuseUpdatedViewHolder(RecyclerView.ViewHolder viewHolder) {
return true;
}
@Override
public boolean animateChange(@NonNull RecyclerView.ViewHolder oldHolder,
@NonNull final RecyclerView.ViewHolder newHolder,
@NonNull ItemHolderInfo preInfo,
@NonNull ItemHolderInfo postInfo) {
Log.d("test", "Run custom animation.");
final ColorsAdapter.ColorViewHolder holder = (ColorsAdapter.ColorViewHolder) newHolder;
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.tvColor.getLayoutParams();
ObjectAnimator halfSize = ObjectAnimator.ofInt(holder.tvColor.getLayoutParams(), "height", params.height, 0);
halfSize.start();
return super.animateChange(oldHolder, newHolder, preInfo, postInfo);
}
Run Code Online (Sandbox Code Playgroud)
现在我只想尝试动画,但没有任何反应......任何想法?
我有一个带有“assets”文件夹的模块,该文件夹与我的 pubspec.yaml 文件位于同一目录中。在我的资产文件夹中,我有 test.txt 和 simpleObject.json。
flutter:
assets:
- assets/test.txt
- assets/simpleObject.json
Run Code Online (Sandbox Code Playgroud)
我相信以下代码应该允许我将其读入我的应用程序。
var test = await DefaultAssetBundle.of(context).loadString("assets/test.txt");
Run Code Online (Sandbox Code Playgroud)
可悲的是,我收到以下错误:
[ERROR:flutter/lib/ui/ui_dart_state.cc(148)] 未处理的异常:无法加载资产:assets/test.txt
错误来自于 asset_bundle.dart。我必须假设这是我的错,但根据我读过的所有内容,我做对了。有什么想法吗?
如果有帮助,这是我的文件结构。
MyModule
|_assets/test.txt
|_lib/
|_pubspec.yaml
Run Code Online (Sandbox Code Playgroud) 在 Android 中,我经常想要导航以响应 ViewModel 的状态更改。(例如,成功的身份验证会触发导航到用户的主屏幕。)
最佳实践是从 ViewModel 内触发导航吗?是否有有意的机制来触发可组合项中的导航以响应 ViewModel 状态更改?
使用 Jetpack Compose 处理此用例的过程并不明显。如果我尝试类似以下示例的操作,将会发生导航,但我导航到的目的地将无法正常运行。我相信这是因为在调用导航之前不允许完成原始的可组合函数。
// Does not behave correctly.
@Composable fun AuthScreen() {
val screenState = viewModel.screenState.observeAsState()
if(screenState.value is ScreenState.UserAuthenticated){
navController.navigate("/gameScreen")
} else {
LoginScreen()
}
}
Run Code Online (Sandbox Code Playgroud)
如果我使用 LaunchedEffect ,我确实会观察到正确的行为,如下所示:
// Does behave correctly.
@Composable fun AuthScreen() {
val screenState = viewModel.screenState.observeAsState()
if(screenState.value is ScreenState.UserAuthenticated){
LaunchedEffect(key1 = "test") {
navController.navigate("$/gameScreen")
}
} else {
LoginScreen()
}
}
Run Code Online (Sandbox Code Playgroud)
它是否正确?LaunchedEffect 的文档说明如下,但我并不是 100% 清楚其含义:
当 LaunchedEffect 进入组合时,它将启动块到组合的 CoroutineContext 中。当 LaunchedEffect 用不同的 key1、key2 或 …
android android-jetpack-compose jetpack-compose-navigation compose-recomposition
运行AndroidStudio 3.6.2。我多次尝试创建新的片段/活动、创建布局 xml 文件,但是当我尝试使用布局 AS 时显示“未解析的引用”错误。“解决”问题的一种选择是让 AS 为我创建布局文件。当我选择此选项时,文件已创建,但 AS 仍然表示无法解析对刚刚创建的文件的引用。注意:它会很好地找到其他布局文件,因此这似乎不是我导入资源的方式。
这是一个已知的问题?
这是我创建的布局文件的内容,以防有人发现它有帮助。
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/content_video"
android:layout_width="4dp"
android:layout_height="2dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/switchVideoCamera"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_switch_camera_white_24dp" />
<ImageButton
android:id="@+id/display_video"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_videocam_white_24dp" />
<ImageButton
android:id="@+id/mute_audio"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@android:color/transparent"
app:srcCompat="@drawable/ic_mic_white_24dp" />
<ImageButton
android:id="@+id/connectActionFab"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_margin="8dp"
android:background="@android:color/transparent"
app:srcCompat="@android:drawable/stat_sys_phone_call" />
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)