如何在Android RelativeLayout中浮动图像?
-
13-11-2019 - |
题
我想显示一个文本,并在文本旁边显示一个动态数量的图像,应该浮动到右侧。
这就是我所拥有的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:padding="7dp"
android:textSize="18sp"
android:textColor="#000"/>
<ImageView
android:id="@+id/icon1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="right"
android:layout_alignParentRight="true"
android:layout_marginTop="10dip"
android:layout_marginRight="6dip"
android:src="@drawable/bus" />
<ImageView
android:id="@+id/icon2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_toLeftOf="@+id/1"
android:layout_marginTop="10dip"
android:layout_marginRight="6dip"
android:src="@drawable/tram" />
</RelativeLayout>
我的问题是,如果 icon1
不可见(我在我的Java代码上控制)icon2不再显示在右侧。它重叠了 text1
, ,因为引用的 icon1
失踪了。
解决方案
好吧,要么你上面的布局不完整(如果是这样,你应该发布完整的东西),要么你的id被破坏(你已经布局了 icon2
在 @+id/1
当它应该在左边 @+id/icon1
.
尝试设置 android:layout_alignWithParentIfMissing="true"
上 icon2
并更正布局id。
不隶属于 StackOverflow