在 CoordinatorLayout 中展开两个相互对立的视图

问题描述 投票:0回答:0

我的协调器布局中有两个视图,基本上是图像视图和 nestedScrollView(作为底部工作表行为)。

当底页上下滑动时,图像应该相应地调整大小,但目前不是,我不确定为什么。我怎样才能让图像正确调整大小?

  • 这是我的示例布局
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
    android:background="#CFCACA"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/cartoon"
        android:layout_marginBottom="50dp"/>


    <androidx.core.widget.NestedScrollView
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_insetEdge="bottom"
        app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="50dp"
                android:layout_marginStart="50dp"
                android:orientation="horizontal">

                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:src="@drawable/ic_baseline_volume_up_24"/>

                <ImageButton
                    android:layout_width="0dp"
                    android:layout_height="40dp"
                    android:backgroundTint="@color/white"
                    android:layout_marginStart="50dp"
                    android:layout_marginEnd="50dp"
                    android:src="@drawable/ic_baseline_play_arrow_24"
                    android:background="@drawable/img_button_shape"
                    android:layout_weight="1"
                    android:layout_gravity="center"
                    />


                <ImageView
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_gravity="end"
                    android:src="@drawable/ic_baseline_volume_up_24"/>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout"
                android:layout_width="match_parent"
                android:layout_height="355dp"
                android:minHeight="100dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/curvy_layout"
                android:orientation="vertical">

                <View
                    android:id="@+id/view"
                    android:layout_width="50dp"
                    android:layout_height="5dp"
                    android:background="@color/black"
                    android:layout_gravity="center"
                    android:layout_marginTop="15dp"/>

                <com.google.android.material.tabs.TabLayout
                    android:id="@+id/tabLayout"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    app:tabMode="scrollable"
                    app:tabBackground="@drawable/tab_bg"
                    app:tabSelectedTextColor="@color/white"
                    app:tabIndicatorHeight="0dp"
                    android:layout_marginTop="20sp"
                    android:layout_marginStart="20dp"
                    android:layout_marginEnd="20dp"/>

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/verticalView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scrollbars="vertical"
                    android:layout_marginTop="30dp"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"
                    android:layout_marginStart="10sp"
                    android:layout_marginEnd="10dp"/>

            </LinearLayout>


        </LinearLayout>

    </androidx.core.widget.NestedScrollView>



</androidx.coordinatorlayout.widget.CoordinatorLayout>

  • 这是我的代码

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        View bottomSheet = findViewById(R.id.bottom_sheet);
        LinearLayout layout = findViewById(R.id.layout);
        View view = findViewById(R.id.view);
        TabLayout tabLayout = findViewById(R.id.tabLayout);
        RecyclerView recyclerView = findViewById(R.id.verticalView);
        imageView = findViewById(R.id.img);
        mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);

        mBottomSheetBehavior.setHideable(false);
        mBottomSheetBehavior.setPeekHeight(layout.getMinimumHeight());

        tabLayout.setBackgroundColor(Color.TRANSPARENT);
        
        // Setting up recycler view

        GridLayoutManager layoutManager = new GridLayoutManager(this,4);
        assert recyclerView != null;
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        bottomSheet.setNestedScrollingEnabled(false);

        getTabsList();
        // Setting Up TabLayout
        assert tabLayout != null;
        createTabLayout(tabLayout,recyclerView);


    }

java android android-coordinatorlayout
© www.soinside.com 2019 - 2024. All rights reserved.