问题是,当我在 EditText 中输入文本时,ScrollView 不起作用,并且不会在我的活动中滚动
fun statusBarColor(activity: Activity) {
val window = activity.window
// add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS)
// clear FLAG_TRANSLUCENT_STATUS flag:
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
//window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
// finally change the color
//window.statusBarColor = ContextCompat.getColor(activity, R.color.colorPrimaryDark)
//window.setStatusBarColor(Color.TRANSPARENT);
}
//Manifest code on activity.
android:windowSoftInputMode="adjustResize">
您可以使用 CoordinatorLayout 作为根,并在其中使用 NestedScrollView 。
CoordinatorLayout 作为根对键盘做出反应,并在 NestedScrollView 的助手下将布局滚动到手机顶部,其中您的 NestedScrollView 包含您的布局代码。
正确看待问题
协调布局 > NestedScrollView > yourLayout
更改布局XMl,如下面的代码
<android.support.design.widget.CoordinatorLayout
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.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- rest of your layout xml code-->
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
您可以通过活动和内容布局文件来做到这一点。从您的示例中,我们可以有 Activity_login 和 content_login。在此示例中,我假设您的活动名为
LoginActivity.class
,因此请相应更新。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".LoginActivity">
<include layout="@layout/content_user_profile" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
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:animateLayoutChanges="true"
android:background="@color/color_white"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".LoginActivity"
tools:showIn="@layout/activity_login">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--Have all your layout here as scrollviews expect one child. You can replace LinearLayout above with constraintLayout for more flexibility and responsiveness.-->
</Linearlayout>
</androidx.core.widget.NestedScrollView>
<activity
android:name=".LoginActivity"
android:colorMode="wideColorGamut"
android:exported="true"
android:label="@string/title_activity_login"
android:theme="@style/..."
android:windowSoftInputMode="adjustPan" />
getWindow().setBackgroundDrawableResource(R.drawable.iv_background);
将
initViews()
设置为 SignIn
活动并使其透明:
<item name="colorPrimaryVariant">#00000000</item>
回复晚了,但可能对某人有帮助。
使用此代码使状态栏透明并设置状态栏和导航栏文本和图标颜色(根据您的需要更改导航栏属性):
WindowCompat.setDecorFitsSystemWindows(window, false)
window.statusBarColor = Color.TRANSPARENT
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars =
false
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightNavigationBars =
!isUsingNightModeResources()
这个监听器可以手动设置插入(例如当显示键盘时):
ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.ime())
view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
updateMargins(insets.left, insets.top, insets.right, insets.bottom)
}
WindowInsetsCompat.Builder().setInsets(WindowInsetsCompat.Type.ime(), insets).build()
}
并在需要时使用它来恢复它:
if (!isUsingNightModeResources()) {
WindowInsetsControllerCompat(window, window.decorView).isAppearanceLightStatusBars =
true
}
WindowCompat.setDecorFitsSystemWindows(window, true)
window.statusBarColor = getColor(R.color.your_color)
isUsingNightModeResources 是这种情况下的扩展,但您可能不需要它:
fun Context.isUsingNightModeResources(): Boolean {
return when (resources.configuration.uiMode and
Configuration.UI_MODE_NIGHT_MASK) {
Configuration.UI_MODE_NIGHT_YES -> true
Configuration.UI_MODE_NIGHT_NO -> false
Configuration.UI_MODE_NIGHT_UNDEFINED -> false
else -> false
}
}
此解决方案适用于 api >= 21