为什么列表视图的顶部有一个白色的空白?

时间:2019-12-10 16:05:14

标签: android listview kotlin

当我的应用程序运行时,自定义操作栏和列表视图之间会有一个空白,我想删除它,但找不到任何类方法或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>

App with blank white space

这是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>

2 个答案:

答案 0 :(得分:2)

问题出在export default function Logs({ store: { status, filteredStatus } }) { const [filter, setFilter] = React.useState('') clearLogs = () => { ... } ... }

行中
android:paddingTop="?attr/actionBarSize"

答案 1 :(得分:-1)

您可以使用布局检查器调试视图。要打开布局检查器,请执行以下操作:

  • 在连接的设备或仿真器上运行您的应用。
  • 点击工具> 布局检查器
  • 在出现的“选择流程”对话框中,选择要检查的应用程序流程 然后点击确定

有关更多信息: https://developer.android.com/studio/debug/layout-inspector