【问题标题】:How to make SwipeRefreshLayout wrap_content?如何使 SwipeRefreshLayout wrap_content?
【发布时间】:2019-02-12 11:26:55
【问题描述】:

如何让 SwipeRefreshLayout 包裹内容?

这是我的布局 mydialog_fragmet.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    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="wrap_content">

    <LinearLayout
        android:id="@+id/contentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/appBackgroundColorLight"
        android:orientation="vertical"
        >

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingTop="16dp"
            android:scrollbarSize="2dp"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbarThumbVertical="@color/colorAccent"
            />

        <include layout="@layout/view_add_place_button"/>

    </LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>

结果 LinearLayout id/contentLayout 变为 match_parent。这是截图:

但是当我使用没有SwipeRefreshLayout的布局时,内容是wrap_contentmydialog_fragmet.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
            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="wrap_content"
            android:background="@color/appBlue"
            android:orientation="vertical"
            >

            <android.support.v7.widget.RecyclerView
                android:id="@+id/placesRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:paddingTop="16dp"
                android:scrollbarSize="2dp"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbarThumbVertical="@color/colorAccent"
                app:maxHeight="300dp"
                />

            <include layout="@layout/view_add_place_button"/>

        </LinearLayout>

这是正确的结果:

【问题讨论】:

    标签: android android-layout android-recyclerview swiperefreshlayout


    【解决方案1】:

    这种特定行为的解决方案是将视图包装在父布局中,仅当您希望它具有该尺寸时才建议使用特定 dp

    <LinearLayout
     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:background="@color/appBackgroundColorLight"
            android:orientation="vertical">
    
       <android.support.v4.widget.SwipeRefreshLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
    
        <LinearLayout
            android:id="@+id/contentLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/appBackgroundColorLight"
            android:orientation="vertical"
            >
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipToPadding="false"
                android:paddingTop="16dp"
                android:scrollbarSize="2dp"
                android:scrollbarStyle="outsideOverlay"
                android:scrollbarThumbVertical="@color/colorAccent"
                />
    
            <include layout="@layout/view_add_place_button"/>
    
        </LinearLayout>
    </android.support.v4.widget.SwipeRefreshLayout>
    

    【讨论】:

      【解决方案2】:

      找到解决方案:

      <LinearLayout
          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:orientation="vertical"
          >
      
          <com.example.views.MaxHeightSwipeRefreshLayout
              android:id="@+id/swipeRefreshLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:maxHeight="382dp">//size 382 = 300(RecyclerView) + 82 (addButton)
      
              <com.example.views.MaxHeightNestedScrollView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:maxHeight="382dp">
      
                  <LinearLayout
                      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="wrap_content"
                      android:background="@color/appBackgroundColorLight"
                      android:orientation="vertical"
                      tools:background="@color/appBlue"
                      >
      
                      <com.example.views.MaxHeightRecyclerView
                          android:id="@+id/placesRecyclerView"
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:clipToPadding="false"
                          android:paddingTop="16dp"
                          android:scrollbarSize="2dp"
                          app:maxHeight="300dp"
                          android:scrollbarStyle="outsideOverlay"
                          android:scrollbarThumbVertical="@color/colorAccent"
                          />
      
                      <include layout="@layout/view_add_place_button"/>
      
                  </LinearLayout>
              </com.example.views.MaxHeightNestedScrollView>
      
          </com.example.views.MaxHeightSwipeRefreshLayout>
      
      </LinearLayout>
      

      MaxHeight* 视图来自https://stackoverflow.com/a/48728490/2425851

      【讨论】:

      • 设置静态数据库值不是一个非常可靠的解决方案。 1db的大小随着屏幕设备的dpi而变化。
      • @Iaroslav Ternovyi 1dp 的大小在不同像素密度的手机中以 px 的数量变化,dp 的存在就是为了:在不同的屏幕密度下具有相同的大小。所以使用“dp静态值”并没有错,使用它们来替换“wrap_content”或“match_parent”预期行为是错误的。
      猜你喜欢
      • 2023-03-11
      • 1970-01-01
      • 2021-01-26
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2015-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多