【问题标题】:How to make Coordinator Layout Scrollable?如何使协调器布局可滚动?
【发布时间】:2017-10-12 12:40:46
【问题描述】:

我使用 Coordinator Layout 作为父布局来锚定一个图像,使用 Linear 布局作为子布局。所以可能的问题是我怎样才能使屏幕可滚动?线性布局分为两个具有权重的孩子。请在下面找到xml代码:

我已经尝试使用嵌套滚动视图和普通滚动视图进行此操作,但屏幕划分会受到干扰,这是通过线性布局权重总和属性完成的。

<android.support.design.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="match_parent"
android:background="@drawable/bg_welcome">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="10"
    tools:context="com.rblbank.mobank.chequebook.fragments.NewChequeBookFragment">

    <TextView
        android:id="@+id/tvNewChequeBook"
        style="?attr/title_1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="4"
        android:gravity="center"
        android:padding="@dimen/activity_horizontal_margin"
        android:text="@string/new_cheque_book_title_note"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="6"
        android:background="@color/white_color"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:padding="@dimen/padding_12">

        <TextView
            android:id="@+id/tvNewChequeBookInvestmentAccountTitle"
            style="?attr/title_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:text="@string/investment_account"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
            android:textColor="@color/black_color" />

        <TextView
            android:id="@+id/tvNewChequeBookInvestmentAccount"
            style="?attr/newChequeBookInvestmentAccount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:textColor="@color/arrow_grey" />

        <View
            android:id="@+id/vwAccountDetailsHorizontalDivider"
            android:layout_width="match_parent"
            android:layout_height="@dimen/divider_width"
            android:layout_marginBottom="@dimen/minimum_margin"
            android:layout_marginTop="@dimen/minimum_margin"
            android:background="@color/home_functionality_divider" />

        <TextView
            android:id="@+id/tvNewChequeBookAddressNote"
            style="?attr/newChequeBookAddressNoteStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin" />


        <TextView
            android:id="@+id/tvNewChequeBookAddress"
            style="?attr/newChequeBookAddress"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin" />

        <Button
            android:id="@+id/btnewChequeBookDeliverCheque"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:text="@string/deliver_cheque_book_to_above_address" />

        <TextView
            android:id="@+id/tvNewChequeBookAddressChanged"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:text="@string/postal_address_has_changed" />

    </LinearLayout>
</LinearLayout>


<ImageView
    android:id="@+id/ivNewChequeBookPostBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_action_submit"
    app:layout_anchor="@id/tvNewChequeBook"
    app:layout_anchorGravity="center_horizontal|bottom"
    app:layout_behavior="com.rblbank.mobank.ScrollAwareFABBehavior" />
</android.support.design.widget.CoordinatorLayout>

【问题讨论】:

    标签: android android-coordinatorlayout


    【解决方案1】:

    让你的布局像这样。

       <CoordinatorLayout>
           <AppBarLayout>
    
           //Toolbar and other contents will come here
    
          </AppBarLayout>
    
        <Recycler Or Nested Scroll View>
    
           <LinearLayout
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
         //This is used when you have a collapsing toolbar and you need to add a scrolling behaviour for your views.
    
           <LinearLayout/>
    
       <Recycler Or Nested Scroll View/>
    

    此外,如果您不使用折叠工具栏或其他动画,您可以在 AppBarLayout 本身内添加视图。

    【讨论】:

    • 感谢您的回答,我也试过了,但线性布局被分成 2 个子元素,权重,所以这对我不起作用。
    【解决方案2】:

    像这样使用滚动视图

    <CoordinatorLayout>
    
     <ScrollView 
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:fillViewport="true">
    
       <LinearLayout>
    
       </LinearLayout>
    
      </ScrollView>
    <CoordinatorLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-31
      • 1970-01-01
      • 1970-01-01
      • 2016-10-06
      相关资源
      最近更新 更多