Android相对布局的左下角与图像的中心位置相同
我有一个带有矩形图像的布局和一个带有图像和文本视图的相对布局。此布局将包含在另一个布局中。 我希望布局的左下角与矩形图像的中心位置相同 在android studio设计预览中,我通过以下布局实现了这一点Android相对布局的左下角与图像的中心位置相同,android,Android,我有一个带有矩形图像的布局和一个带有图像和文本视图的相对布局。此布局将包含在另一个布局中。 我希望布局的左下角与矩形图像的中心位置相同 在android studio设计预览中,我通过以下布局实现了这一点 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_w
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_center_image"
android:src="@drawable/my_center_image"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="1px"
android:layout_height="1px"
android:id="@+id/placeholder"
android:layout_centerInParent="true"
android:background="@null"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/second_placeholder"
android:layout_toRightOf="@+id/placeholder"
android:layout_above="@+id/placeholder">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_image"
android:background="@drawable/my_image"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:id="@+id/count"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:textSize="10dp"/>
</RelativeLayout>
</RelativeLayout>
在真实设备中,这并没有正确显示,我所能达到的模仿我想要的最佳效果是以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_center_image"
android:src="@drawable/my_center_image"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/placeholder"
android:layout_marginLeft="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_image"
android:background="@drawable/my_image"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:id="@+id/count"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:textSize="10dp"/>
</RelativeLayout>
</RelativeLayout>
有人能帮我理解我应该怎么做才能将版面的左下角放在图像的中心吗?
我应该怎么做才能将版面的左下角放在图像的中心?
您可以将通用视图(1dp宽,1dp高)放置在屏幕中央。将ImageView也放在中间。
通用视图不可见,但必须具有唯一ID。
然后将布局设置为视图的右侧和顶部 如下图所示:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<View
android:layout_width="1dp"
android:layout_height="1dp"
android:id="@+id/my_center_view"
android:layout_centerInParent="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_center_image"
android:src="@drawable/my_center_image"
android:layout_centerInParent="true"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/placeholder"
android:layout_toRightOf="my_center_view"
android:layout_above="my_center_view"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_image"
android:background="@drawable/my_image"
android:layout_centerInParent="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="20"
android:id="@+id/count"
android:textColor="@android:color/white"
android:layout_centerInParent="true"
android:textSize="10dp"
/>
</RelativeLayout>
</RelativeLayout>
这听起来很复杂,但事实并非如此。它只是利用了RelativeLayout子项继承的相对性。
我应该怎么做才能将布局的左下角放在图像的中心?
您可以将通用视图(1dp宽,1dp高)放在屏幕的中心。将ImageView也放在中间。常规视图不可见,但必须具有唯一ID。然后将布局设置为视图的右侧和顶部。这听起来很复杂,但事实并非如此。它只是利用了年轻人所继承的相对性。谢谢你的第一个建议。这删除了一些有用的代码:)您可以在第一个xml中看到,@+id/placeholder
正试图实现您的建议。不幸的是,它没有达到预期的效果。您想试用我的代码吗?另外,我注意到TextView和图像叠加在第二个RelativeLayout的中心。这是期望的效果还是图像可能位于文本的一侧?在这种情况下,您可以去掉RelativeLayout和ImageView,并将图像合并到TextView中作为一个复合可绘制的。您好,很抱歉响应太晚。我意识到我在另一个布局中包含了此布局,并将父级高度更改为match\u parent
而不是wrap\u content
解决了我的问题。