【问题标题】:Custom view with horizontalScrollview in RecyclerView match_parent not workingRecyclerView match_parent 中带有 Horizo​​ntalScrollview 的自定义视图不起作用
【发布时间】:2018-12-12 10:11:11
【问题描述】:

我有一个包含我的自定义视图的RecyclerView。 我的自定义视图没有得到所有宽度,所以我将其配置为match_parent. 我想要的是水平滚动视图将占据所有屏幕,并且卡片将占据屏幕宽度(因此黄色部分将在开始时留在外面)

我尝试使用LayoutInflater.from(getContext()).inflate(R.layout.view_home_screen_card, (ViewGroup) this, false),,但没有成功

我还尝试以编程方式设置宽度,但我在几毫秒内看到了错误的大小,然后它就会改变并且看起来很丑。

奇怪的是,当我取出水平滚动视图时,视图全部到位。

附上布局

 <?xml version="1.0" encoding="utf-8"?> <merge
 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">

     <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:id="@+id/horizontal_scroll_view"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:fillViewport="true"
         android:scrollbars="none">

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


             <ImageView
                 android:layout_width="100dp"
                 android:layout_height="150dp"
                 android:background="@color/amber_200" />

             <android.support.v7.widget.CardView
                 android:id="@+id/cardView"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 android:layout_margin="@dimen/half_unit"
                 app:cardCornerRadius="@dimen/8dp">


             </android.support.v7.widget.CardView>


         </LinearLayout>

     </HorizontalScrollView>


 </merge>

还有什么想法吗?

【问题讨论】:

  • 请分享您的自定义布局代码
  • @RahulChokshi 我已经编辑了问题

标签: android layout android-recyclerview


【解决方案1】:

你的 recyclerview 有 match_parent 宽度吗?如果没有,那就做吧。

【讨论】:

  • 回收视图匹配父级并占用所有屏幕尺寸
  • 你能分享你的自定义布局吗
  • 尝试从cardview中删除layout_margin和从imageview中删除背景色
  • 试过了,没用
  • 您能否发布该布局的预期输出和实际输出的屏幕截图
猜你喜欢
  • 2012-03-10
  • 2018-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 2017-11-10
相关资源
最近更新 更多