android:layout_alignParentBottom 在运行时不工作

标签 android android-recyclerview android-relativelayout

我在 RelativeLayout 中为 RecyleView 的每个项目使用 android:layout_alignParentBottom 作为向下箭头 ImageView
但它只在 Preview 模式下完美运行,当我运行应用程序时它不起作用。我不知道为什么会这样。
我已经在截图中详细解释了我的问题

预览中
enter image description here
在运行时
enter image description here

 <android.support.v7.widget.CardView
        android:id="@+id/cv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

            <LinearLayout
                android:id="@+id/item_layoutStart"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#E8F5E9"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />
                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_gravity="center_vertical|right"
                    android:clickable="true"
                    android:scaleType="fitCenter"
                    android:src="@mipmap/ic_launcher" />
                <TextView
                    android:id="@+id/item_tvRepeatTime"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="10" />
            </LinearLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ff0"
                android:layout_alignTop="@id/item_layoutStart"
                android:layout_alignBottom="@id/item_layoutStart"
                android:orientation="vertical"
                android:layout_toRightOf="@id/item_layoutStart">
                <TextView
                    android:id="@+id/item_tvTitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa " />
                <TextView
                    android:id="@+id/item_tvResult"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/item_tvTitle"
                    android:text="aa aa aaaaaa aa aa aaaaaa aa aa aaaaaa" />
                <ImageView android:id="@+id/item_ivExpand"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentBottom="true"
                    android:padding="3dp"
                    android:src="@drawable/selector_arrow_down"/>
            </RelativeLayout>
        </RelativeLayout>
    </android.support.v7.widget.CardView>

更新 如果我通过特定值固定我的 RelativeLayout 的高度,android:layout_alignParentBottom work

 <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
 > 

但在我的例子中,我希望 RelativeLayout 高度等于 LinearLayout

<RelativeLayout
                android:layout_alignTop="@id/item_layoutStart"
                android:layout_alignBottom="@id/item_layoutStart"
               >

android:layout_alignParentBottom 不工作

如有任何帮助,我们将不胜感激

最佳答案

虽然这个解决方案明显扰乱了编辑器中布局的外观,但它解决了运行时布局的问题。

首先,将外部 RelativeLayout 更改为 LinearLayout。然后,在内部 RelativeLayout 上,一起删除 layout_alignToplayout_alignBottom 属性,并将 layout_height 设置为 匹配父级

正如您在发布的屏幕截图中看到的那样,ImageView 的布局就好像其父级 RelativeLayout 的高度包裹着其子级 Views,根据其 layout_height 设置。然而,在 RelativeLayout 的子 View 布局之后,它的 layout_alignToplayout_alignBottom 属性被应用, 并且调整大小以匹配 LinearLayout 的高度,但是 ImageView 只是停留在原来的位置。

通过为外部 ViewGroup 使用 LinearLayout,并将内部 RelativeLayout 的高度设置为 match_parentRelativeLayout 在布置其子 View 之前应用了确定的高度,而 ImageViewlayout_alignParentRightlayout_alignParentBottom 属性使其位于正确的位置。

关于android:layout_alignParentBottom 在运行时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36823168/

相关文章:

滚动顶部时Android RecyclerView StaggeredGrid项目改变位置

android - 动态添加内容到 RelativeLayout

java - 以编程方式在布局中添加 View 并将 "clustered"添加到中心

android - 完全限定的数据库路径名的 SQLiteOpenHelper 问题

关于 png 图像的 Android 内存不足

java - 具有固定数量子项的 CardView

android - 处理 RelativeLayout 的更好方法

android - 在 Android 上为多语言更改 ImageView src

android 更改底部栏中的默认项目

带有 SectionIndexer 的 Android RecyclerView