android:layout_alignParentBottom 运行时不工作
android:layout_alignParentBottom not working in runtime
对于 RecyleView
的每个项目,RelativeLayout
中的向下箭头 ImageView
使用 android:layout_alignParentBottom
。
但它只在 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
设置为 match_parent
。
正如您在发布的屏幕截图中看到的那样,ImageView
的布局就像其父级 RelativeLayout
的高度包裹其子级 View
一样,根据其layout_height
设置。但是,在 RelativeLayout
的子 View
布局后,应用了其 layout_alignTop
和 layout_alignBottom
属性,并调整了大小以匹配 LinearLayout
的高度,但 ImageView
只是停留在原来的位置。
通过使用 LinearLayout
作为外部 ViewGroup
,并将内部 RelativeLayout
的高度设置为 match_parent
,RelativeLayout
具有明确的在它布置其子 View
之前应用的高度,并且 ImageView
的 layout_alignParentRight
和 layout_alignParentBottom
属性将其放置在正确的位置。
对于 RecyleView
的每个项目,RelativeLayout
中的向下箭头 ImageView
使用 android:layout_alignParentBottom
。
但它只在 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
设置为 match_parent
。
正如您在发布的屏幕截图中看到的那样,ImageView
的布局就像其父级 RelativeLayout
的高度包裹其子级 View
一样,根据其layout_height
设置。但是,在 RelativeLayout
的子 View
布局后,应用了其 layout_alignTop
和 layout_alignBottom
属性,并调整了大小以匹配 LinearLayout
的高度,但 ImageView
只是停留在原来的位置。
通过使用 LinearLayout
作为外部 ViewGroup
,并将内部 RelativeLayout
的高度设置为 match_parent
,RelativeLayout
具有明确的在它布置其子 View
之前应用的高度,并且 ImageView
的 layout_alignParentRight
和 layout_alignParentBottom
属性将其放置在正确的位置。