为什么列表视图的顶部会有白色的空白?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了为什么列表视图的顶部会有白色的空白?相关的知识,希望对你有一定的参考价值。
当我的应用程序运行时,我的自定义操作栏和列表视图之间有一个空白区域,我想删除它,但找不到任何类方法或xml设置来做到这一点。
这是我的使用列表视图的Kotlin代码EnglandFragment.kt
:
class EnglandFragment : Fragment()
// Access a Cloud Firestore instance from your Activity
val db = FirebaseFirestore.getInstance()
lateinit var adapter : ArrayAdapter<String>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View?
val root = inflater.inflate(R.layout.fragment_england, container, false)
(requireActivity() as CountriesActivity).initializeCustomActionBar(R.drawable.england_flag, R.string.title_counties)
var counties : ArrayList<String>
val docRef = db.collection("UKSites").document("England")
docRef.get()
.addOnSuccessListener document ->
if (document != null)
counties = document.get("Counties") as ArrayList<String>
adapter = ArrayAdapter(requireContext(), R.layout.list_item_view, counties)
groupListView.adapter = adapter
groupListView.setOnItemClickListener parent, view, position, id ->
Toast.makeText(requireContext(), "Row selected: $position", Toast.LENGTH_SHORT ).show()
else
Log.d("Debug", "No such document")
.addOnFailureListener exception ->
Log.d("Debug", "get failed with ", exception)
return root
这是我的列表视图的xml代码fragment_england.xml
:
<?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">
<ListView
android:id="@+id/groupListView"
android:layout_width="0dp"
android:layout_height="0dp"
android:headerDividersEnabled="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
这里是CountriesActivity.kt
的Kotlin代码:
class CountriesActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_countries)
val navView: BottomNavigationView = findViewById(R.id.nav_view)
navView.setItemIconTintList(null);
val navController = findNavController(R.id.nav_host_fragment)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_england, R.id.navigation_scotland, R.id.navigation_wales, R.id.navigation_nireland
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
navView.setupWithNavController(navController)
fun initializeCustomActionBar(imageViewResID: Int, textViewResID: Int)
val actionBar: ActionBar? = this.supportActionBar
actionBar?.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
actionBar?.setDisplayShowCustomEnabled(true)
actionBar?.setCustomView(R.layout.custom_action_bar)
val view : View? = actionBar?.customView
var imageView : ImageView? = view?.findViewById(R.id.flagImageView)
imageView?.setImageResource(imageViewResID)
var textView : TextView? = view?.findViewById(R.id.countiesTextView)
textView?.text = getString(textViewResID)
actionBar?.setCustomView(view)
这是activity_countries.xml
的xml代码:
<?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"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
答案
问题出在android:paddingTop="?attr/actionBarSize"
行中
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
</androidx.constraintlayout.widget.ConstraintLayout>
另一答案
您可以使用布局检查器调试视图。要打开布局检查器,请执行以下操作:
- 在连接的设备或仿真器上运行您的应用程序。
- 单击工具> 布局检查器。
- 在出现的“选择流程”对话框中,选择要检查的应用程序流程然后单击确定
有关更多信息:https://developer.android.com/studio/debug/layout-inspector
以上是关于为什么列表视图的顶部会有白色的空白?的主要内容,如果未能解决你的问题,请参考以下文章