在Android中的水平线上添加ImageView

时间:2018-02-09 05:51:24

标签: android android-layout

我想创建包含水平线(点线)和的布局 在那一行,我想添加水平居中的小图像

这是我的布局文件

<RelativeLayout
    android:id="@+id/widget38"
    android:layout_width="51dp"
    android:layout_height="wrap_content">

      <View
         android:layout_width="51dp"
         android:layout_height="1dp"
         android:background="@color/gray" />

      <ImageView
         android:id="@+id/image2"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:gravity="center_horizontal"
         android:layout_marginTop="-5dp"
         android:src="@drawable/image"/>

  </RelativeLayout>

添加负边距以将该图像置于行上。但我的问题是图像(部分在线上方)上部不可见。

我也试过使用framelayout。

但获得相同的结果

我不想使用约束布局

我试过这个链接 Android: Overlapping two views (intentionally!)

https://www.android-examples.com/overlap-view-by-putting-another-view-above-in-relativelayout-android/

怎么做

3 个答案:

答案 0 :(得分:1)

为视图指定上边距。您的视图位于相对布局的顶部。因此,当您尝试将图像放在线上时,它将超出相对布局。

答案 1 :(得分:0)

您可以将LinearLayout用于此目的,如下所示:

<LinearLayout
                android:id="@+id/widget38"
                android:layout_width="51dp"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center"
                android:layout_gravity="center"
                >
                <View
                    android:layout_width="51dp"
                    android:layout_height="1dp"
                    android:background="@color/gray" />
                <ImageView
                    android:id="@+id/image2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_horizontal"
                    android:layout_marginTop="-5dp"
                    android:src="@drawable/image"/>
            </LinearLayout>

修改

您必须将Linear的宽度设置为match_parent。

答案 2 :(得分:0)

试试这个...

setTimeout