Android Studio 我的布局不适合框架布局

mwyxok5s  于 2023-08-07  发布在  Android
关注(0)|答案(2)|浏览(111)

请帮助我修复我的布局,我的类别被隐藏在最近的布局下面&它们几乎不显示。我尝试添加ScrollView,但在此代码中显示错误“binding = FragmentHomeBinding.inflate(layoutInflater,container,false)”。我已经附加了快照,以查看布局错误。对不起,我的愚蠢的错误,我是一个新手。
See the category text & Image is barely visible.
fragment_home.xml

<androidx.core.widget.NestedScrollView 
    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"
    tools:context=".HomeFragment">  
 <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/bom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Best of the Month"
        android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv_bomLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />
</LinearLayout>

    <LinearLayout
        android:layout_below="@id/bom"
        android:id="@+id/recent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Recent Wallpapers"
        android:textSize="20sp" />

     <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rcv_recentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp" />
</LinearLayout>
<LinearLayout
        android:layout_below="@id/recent"
        android:id="@+id/cat"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Categories"
            android:textSize="20sp" />
<androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rcv_categories"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp" />
</LinearLayout>
</RelativeLayout>    
</FrameLayout>
</androidx.core.widget.NestedScrollView>

字符串
activity.main.xml

<RelativeLayout
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"
tools:context=".MainActivity"
android:orientation="vertical"> 
<androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <FrameLayout
        android:id="@+id/frame_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

    </FrameLayout> 
</androidx.core.widget.NestedScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:layout_below="@id/nestedScrollview"
    android:layout_alignParentBottom="true"
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:menu="@menu/bottom_nav_menu"
    tools:ignore="MissingConstraints,NotSibling"
    tools:layout_editor_absoluteX="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>

</RelativeLayout>
</RelativeLayout>


首页片段.kt

class HomeFragment : Fragment() {

lateinit var binding: FragmentHomeBinding
lateinit var db: FirebaseFirestore
override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = FragmentHomeBinding.inflate(layoutInflater, container, false)
    db = FirebaseFirestore.getInstance()

    db.collection("bestofthemonth").addSnapshotListener { value, error ->
       val listBestOfTheMonth = arrayListOf<BomModel>()
        val data = value?.toObjects(BomModel::class.java)
        listBestOfTheMonth.addAll(data!!)

        binding.rcvBomLayout.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL,true)
        binding.rcvBomLayout.adapter = BomAdapter(requireContext(), listBestOfTheMonth)

    }

qv7cva1a

qv7cva1a1#

使用ScrollView,尝试从具有android:id="@+id/cat"LinearLayoutandroid:layout_height="wrap_content"更新android:layout_height="match_parent"

lbsnaicq

lbsnaicq2#

您可以先使用scrollview并将framelayout作为子级,因为scrollview只能有1个直接子级

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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"
    android:orientation="vertical"
    tools:context=".HomeFragment">

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        //other layout/views

    </FrameLayout>
</ScrollView>

字符串
如果要在scrollview中包含recyclerview,请使用NestedScrollView代替ScrollView

<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView
    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"
    android:orientation="vertical"
    tools:context=".HomeFragment">

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        //other layout/views

    </FrameLayout>
</androidx.core.widget.NestedScrollView

相关问题