Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ViewPager2中的碎片未填充高度_Android_Kotlin_Android Viewpager_Fragment - Fatal编程技术网

Android ViewPager2中的碎片未填充高度

Android ViewPager2中的碎片未填充高度,android,kotlin,android-viewpager,fragment,Android,Kotlin,Android Viewpager,Fragment,我用TabLayout设置了ViewPager2,以便通过滑动或单击选项卡切换片段。我所知道的问题是,我的碎片并没有占据所有的空间,我在底部有一个无法滑动的空白空间。我怎样才能使我的碎片填满那个空间 编辑:我认为我的问题与我试图在另一个片段中加载我的viewpager有关。我将代码复制到一个新项目中,而不是将代码加载到一个片段中,而是将其加载到主活动中,它成功了。在片段中加载ViewPager是一种不好的做法吗 我已经像这样更改了我的ProfileFragment.kt,但它仍然不起作用: cl

我用TabLayout设置了ViewPager2,以便通过滑动或单击选项卡切换片段。我所知道的问题是,我的碎片并没有占据所有的空间,我在底部有一个无法滑动的空白空间。我怎样才能使我的碎片填满那个空间

编辑:我认为我的问题与我试图在另一个片段中加载我的viewpager有关。我将代码复制到一个新项目中,而不是将代码加载到一个片段中,而是将其加载到主活动中,它成功了。在片段中加载ViewPager是一种不好的做法吗

我已经像这样更改了我的ProfileFragment.kt,但它仍然不起作用:

class ProfileFragment : Fragment() {

    private lateinit var tabsText: List<String>
    private val adapter by lazy {
        ViewPagerAdapter(childFragmentManager, lifecycle)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tabsText = listOf<String>(getString(R.string.user_info),
            getString(R.string.saved_recipes), getString(R.string.shopping_list))
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_profile, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        pager.adapter = adapter

        TabLayoutMediator(tabs, pager) { tab, position ->
            tab.text = tabsText.get(position)
        }.attach()
    }
}

class ViewPagerAdapter(manager: FragmentManager, lifecycle: Lifecycle): FragmentStateAdapter(manager, lifecycle) {
    override fun getItemCount(): Int = 3

    override fun createFragment(position: Int): Fragment = when(position) {
        0 -> UserInfoFragment.newInstance()
        1 -> SavedRecipesFragment.newInstance()
        else -> ShoppingListFragment.newInstance()
    }
}
class-ProfileFragment:Fragment(){
私有lateinit var tabsText:列表
由lazy提供的私有val适配器{
ViewPagerAdapter(子碎片管理器,生命周期)
}
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
tabsText=listOf(getString(R.string.user_info),
getString(R.string.saved_recipes),getString(R.string.shopping_list))
}
覆盖创建视图(
充气机,
容器:视图组?,
savedInstanceState:捆绑?
):查看{
返回充气机。充气(R.layout.fragment_外形,容器,假)
}
覆盖已创建的视图(视图:视图,保存状态:捆绑?){
super.onViewCreated(视图,savedInstanceState)
pager.adapter=适配器
TabLayoutMediator(选项卡,寻呼机){tab,位置->
tab.text=tabsText.get(位置)
}附(
}
}
类ViewPagerAdapter(管理器:FragmentManager,生命周期:lifecycle):FragmentStateAdapter(管理器,生命周期){
重写getItemCount():Int=3
重写片段(位置:Int):片段=何时(位置){
0->UserInfoFragment.newInstance()
1->SavedRecipesFragment.newInstance()
else->ShoppingListFragment.newInstance()
}
}
fragment_profile.xml

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

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"/>

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/username_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:drawableTop="@drawable/ic_account_circle_black_56dp"
        android:text="@string/username" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/init_info_age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/username_text"
        android:layout_marginTop="8dp"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:hint="@string/age"
        app:helperTextEnabled="true"
        app:helperText="@string/fui_required_field">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/init_info_age_editText"
            android:inputType="number"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

    </com.google.android.material.textfield.TextInputLayout>


    <LinearLayout
        android:id="@+id/linear_layout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:layout_below="@id/init_info_age">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/profile_info_height"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"

            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:hint="@string/height"
            app:helperTextEnabled="true"
            app:helperText="@string/fui_required_field"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/profile_info_height_editText"
                android:inputType="number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/profile_info_weight"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:hint="@string/weight"
            app:helperTextEnabled="true"
            app:helperText="@string/fui_required_field"
            >

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/profile_info_weight_editText"
                android:inputType="number"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </com.google.android.material.textfield.TextInputLayout>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear_layout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:layout_below="@id/linear_layout1">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/profile_gender"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:hint="@string/gender"
            >

            <AutoCompleteTextView
                android:id="@+id/autocomplete_view_gender"
                android:labelFor="@id/profile_gender"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                tools:ignore="LabelFor" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/profile_lifestyle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginTop="8dp"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:hint="@string/activity"
            >

            <AutoCompleteTextView
                android:id="@+id/autocomplete_view_lifestyle"
                android:labelFor="@id/profile_gender"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                tools:ignore="LabelFor" />

        </com.google.android.material.textfield.TextInputLayout>

    </LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/edit"
        android:layout_below="@id/linear_layout2"
        android:layout_alignParentEnd="true"/>



</RelativeLayout>


您的ViewPager的高度设置为
包裹内容
,因此它的高度尽可能小。尝试将其设置为
0dp
并设置
android:layout\u weight=“1”

您还可能希望使用ConstraintLayout而不是LinearLayout。尝试:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabMode="fixed" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabs" />

</androidx.constraintlayout.widget.ConstraintLayout>

您的ViewPager的高度设置为
包裹内容
,因此它的高度尽可能小。尝试将其设置为
0dp
并设置
android:layout\u weight=“1”

您还可能希望使用ConstraintLayout而不是LinearLayout。尝试:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabMode="fixed" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabs" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果我将其设置为匹配父项,则片段将覆盖选项卡。应用更改后,我的片段现在不可见。我不明白为什么会这样,因为你的建议对我来说很有意义。你是否使用了
android:layout\u height=“0dp”
android:layout\u weight=“1”
在LinearLayout或
android:layou height=“0dp”
应用程序:layout\u constraintBottom\u toBottomOf=“parent”
应用程序:在ConstraintLayout中布局\u constraintTop\u toBottomOf=“@id/tabs”
?您现在可以发布布局xml的外观了吗?我粘贴了您的全部代码,并从xml文件中删除了我的代码。我用您更新的代码更新了我的示例。它仍然有效。我不知道你的编辑是什么意思。
ProfileFragment
是否嵌入到不同的片段或活动中?如果是不同的片段,那么片段的大小也很重要。如果我将其设置为匹配父项,则片段将覆盖选项卡。应用更改后,我的片段现在不可见。我不明白为什么会这样,因为你的建议对我来说很有意义。你是否使用了
android:layout\u height=“0dp”
android:layout\u weight=“1”
在LinearLayout或
android:layou height=“0dp”
应用程序:layout\u constraintBottom\u toBottomOf=“parent”
应用程序:在ConstraintLayout中布局\u constraintTop\u toBottomOf=“@id/tabs”
?您现在可以发布布局xml的外观了吗?我粘贴了您的全部代码,并从xml文件中删除了我的代码。我用您更新的代码更新了我的示例。它仍然有效。我不知道你的编辑是什么意思。
ProfileFragment
是否嵌入到不同的片段或活动中?如果是不同的碎片,那么碎片的大小也很重要。