在Android中的水平线上添加ImageView
我想创建包含水平线(虚线)和 在这条线上,我想添加一个水平居中的小图像 这是我的布局文件在Android中的水平线上添加ImageView,android,android-layout,Android,Android Layout,我想创建包含水平线(虚线)和 在这条线上,我想添加一个水平居中的小图像 这是我的布局文件 <RelativeLayout android:id="@+id/widget38" android:layout_width="51dp" android:layout_height="wrap_content"> <View android:layout_width="51dp" android:layout_h
<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
但是得到同样的结果
我不想使用约束布局
我试过这个链接
如何执行此操作您可以使用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>
编辑
您必须设置线性的宽度以匹配父项。尝试使用此
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/widget38"
android:layout_width="51dp"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_marginTop="2dp"
android:layout_height="1dp"
android:background="@android:color/black" />
<ImageView
android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image" />
</LinearLayout>
</LinearLayout>
为视图提供上边距。视图将放置在相对布局的顶部。因此,当您尝试将图像放到联机时,它超出了相对布局。您可以发布预期的结果图像吗?我希望使用上述xml获得相同的结果。只是我想显示完整的图像。当前获取图像的一半必须设置“线性”的宽度以匹配父对象。这是不可见的,因为线性布局宽度与视图相同。