我在 RelativeLayout
中为 RecyleView
的每个项目使用 android:layout_alignParentBottom
作为向下箭头 ImageView
。
但它只在 Preview
模式下完美运行,当我运行应用程序时它不起作用。我不知道为什么会这样。
我已经在截图中详细解释了我的问题
<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_alignTop
和 layout_alignBottom
属性,并将 layout_height
设置为 匹配父级
。
正如您在发布的屏幕截图中看到的那样,ImageView
的布局就好像其父级 RelativeLayout
的高度包裹着其子级 View
s,根据其 layout_height
设置。然而,在 RelativeLayout
的子 View
布局之后,它的 layout_alignTop
和 layout_alignBottom
属性被应用, 并且调整大小以匹配 LinearLayout
的高度,但是 ImageView
只是停留在原来的位置。
通过为外部 ViewGroup
使用 LinearLayout
,并将内部 RelativeLayout
的高度设置为 match_parent
,RelativeLayout
在布置其子 View
之前应用了确定的高度,而 ImageView
的 layout_alignParentRight
和 layout_alignParentBottom
属性使其位于正确的位置。
关于android:layout_alignParentBottom 在运行时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36823168/