This commit is contained in:
2026-04-28 14:11:48 +08:00
parent 805c9ef068
commit 827d85724e
8 changed files with 124 additions and 18 deletions

View File

@@ -1,12 +1,17 @@
package com.example.defenseapplication.home
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.util.TypedValue
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.defenseapplication.adapter.DeviceAdapter
import com.example.defenseapplication.adapter.DeviceUi
import com.example.defenseapplication.adapter.FamilyAdapter
@@ -49,6 +54,17 @@ class HomeMenuFragment : Fragment() {
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
// 为 LinearLayout 设置渐变背景
setGradientBackground(
view = binding.gradientLayout,
startColor = Color.parseColor("#EBDCED"),
endColor = Color.parseColor("#CAF4FF")
)
}
private fun initView() {
binding.rvFamilyList.layoutManager = LinearLayoutManager(requireContext())
binding.rvFamilyList.adapter = familyAdapter
@@ -56,6 +72,11 @@ class HomeMenuFragment : Fragment() {
binding.rvDeviceList.layoutManager = GridLayoutManager(requireContext(), 2)
binding.rvDeviceList.adapter = deviceAdapter
if (binding.rvDeviceList.itemDecorationCount == 0) {
binding.rvDeviceList.addItemDecoration(
GridHorizontalSpaceItemDecoration(horizontalSpace = dpToPx(8))
)
}
updateDeviceList()
binding.tvCurrentFamily.text = currentFamily
@@ -94,6 +115,45 @@ class HomeMenuFragment : Fragment() {
deviceAdapter.submitList(familyDeviceMap[currentFamily].orEmpty())
}
private fun dpToPx(dp: Int): Int {
return TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp.toFloat(),
resources.displayMetrics
).toInt()
}
private class GridHorizontalSpaceItemDecoration(
private val horizontalSpace: Int
) : RecyclerView.ItemDecoration() {
override fun getItemOffsets(
outRect: Rect,
view: View,
parent: RecyclerView,
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
if (position == RecyclerView.NO_POSITION) return
if (position % 2 == 0) {
outRect.right = horizontalSpace / 2
} else {
outRect.left = horizontalSpace / 2
}
}
}
private fun setGradientBackground(
view: View,
startColor: Int,
endColor: Int,
orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TL_BR
) {
val gradientDrawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
view.background = gradientDrawable
}
override fun onDestroyView() {
super.onDestroyView()
binding.rvFamilyList.adapter = null

View File

@@ -1,5 +1,7 @@
package com.example.defenseapplication.login
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.os.CountDownTimer
import android.text.InputType
@@ -32,7 +34,10 @@ class ForgetPasswordActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = ForgetPasswordActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
// 设置渐变背景
val startColor = Color.parseColor("#CBEBFA")
val endColor = Color.parseColor("#FFF2EF")
setGradientBackground(binding.main, startColor, endColor)
setupClickListeners()
}
@@ -168,6 +173,16 @@ class ForgetPasswordActivity : AppCompatActivity() {
}
private fun setGradientBackground(
view: android.view.View,
startColor: Int,
endColor: Int,
orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TL_BR
) {
val gradientDrawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
view.background = gradientDrawable
}
override fun onDestroy() {
super.onDestroy()

View File

@@ -1,5 +1,7 @@
package com.example.defenseapplication.login
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import com.example.defenseapplication.R
import android.os.Bundle
import android.os.CountDownTimer
@@ -22,6 +24,11 @@ class LoginActivity : AppCompatActivity() {
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
// 设置渐变背景
val startColor = Color.parseColor("#CBEBFA")
val endColor = Color.parseColor("#FFF2EF")
setGradientBackground(binding.main, startColor, endColor)
setupTabListeners()
setupClickListeners()
switchToSmsMode()
@@ -159,4 +166,15 @@ class LoginActivity : AppCompatActivity() {
}
}.start()
}
private fun setGradientBackground(
view: android.view.View,
startColor: Int,
endColor: Int,
orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TL_BR
) {
val gradientDrawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
view.background = gradientDrawable
}
}

View File

@@ -1,6 +1,8 @@
package com.example.defenseapplication.login
import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
@@ -19,7 +21,10 @@ class RegisterActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
binding = RegisterActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
// 设置渐变背景
val startColor = Color.parseColor("#CBEBFA")
val endColor = Color.parseColor("#FFF2EF")
setGradientBackground(binding.main, startColor, endColor)
setupListeners()
//setupTextWatchers()
}
@@ -187,4 +192,14 @@ class RegisterActivity : AppCompatActivity() {
// private fun validatePassword(password: String): Boolean {
// return password.length in 6..20
// }
private fun setGradientBackground(
view: android.view.View,
startColor: Int,
endColor: Int,
orientation: GradientDrawable.Orientation = GradientDrawable.Orientation.TL_BR
) {
val gradientDrawable = GradientDrawable(orientation, intArrayOf(startColor, endColor))
gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
view.background = gradientDrawable
}
}