【问题标题】:ScrollView starts to low and crops contentScrollView 开始变低并裁剪内容
【发布时间】:2019-10-29 12:44:44
【问题描述】:

我有一个BottomNavigation 活动,我的一个活动是滚动视图,这里是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:id="@+id/table"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp">

        <TableRow>
            <TextView
                android:text="Test"/>
        </TableRow>
        ...
        <TableRow>
            <TextView
                android:text="Test"/>
        </TableRow>
    </TableLayout>
</ScrollView>

如您所见,ScrollView 不是从顶部开始的。 此外,内容会在顶部丢失相同数量的空间。

这似乎是一个简单的问题,有没有人有想法?

按需求:活动代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home, R.id.navigation_activities, R.id.navigation_logs)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(navView, navController);
}

还有activity_main.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>

LayoutInspector 输出:

【问题讨论】:

  • 你也可以添加活动代码吗?另一件事:在 TableLayout 中使用 android:layout_height="match_parent" 而不是 android:layout_height="wrap_content"
  • 您可以使用layout inspector 找出哪个组件实际占用了顶部的空白区域。
  • @EfeBudak 改变高度什么也没做,活动代码目前什么也不做,无论如何都添加了。
  • @danzel 似乎导航栏是在滚动视图之前绘制的,然后再向下推,而滚动视图没有被向上推,知道如何解决这个问题吗? (截图中提供)
  • 你可以试试 android:layout_height="0dp" 的 nav_host_fragment 吗?

标签: android android-scrollview android-tablelayout


【解决方案1】:

我遇到了类似的问题,解决方法是: 删除android:paddingTop="?attr/actionBarSize"。 您还必须在 nav_host_fragment 处添加 android:layout_marginBottom="?attr/actionBarSize",以便视图不在 BottomNavigationView 下方。

<?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">

    <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"
        android:layout_marginBottom="?attr/actionBarSize"
        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】:

移除容器中的这个属性:

android:paddingTop="?attr/actionBarSize"

改成这样可能是个好主意:

  android:paddingBottom="?attr/actionBarSize"

【讨论】:

  • 我什至没有想过改变那个文件,它是由android studio生成的。删除android:paddingTop="?attr/actionBarSize" 就足够了,尽管最后仍然缺少一些东西(我可以通过将android:paddingBottom="60dp" 添加到我的表中来解决这个问题)谢谢!
猜你喜欢
  • 2018-12-06
  • 1970-01-01
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 2018-06-20
  • 2012-07-11
  • 1970-01-01
  • 2012-12-24
相关资源
最近更新 更多