可以在 XML LinearLayout 中添加一个 ImageView,其宽度为屏幕宽度的 15%,高度也完全相同?
我尝试使用当用户单击地图上的标记时显示的此对话框代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/custom_info_bubble"
android:orientation="vertical">
.
. [some more content here]
.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<ImageView
android:id="@+id/favorite"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:adjustViewBounds="true"
android:layout_gravity="center_horizontal"
android:layout_weight="15"
android:layout_marginEnd="5dp" />
</LinearLayout>
</LinearLayout>
但是有两个问题:
问题 1: android:layout_weight="15" 占对话框宽度的 15%,而不是屏幕宽度,这是一个大问题,因为对话框的宽度取决于其内容,有时或多或少。
问题 2:宽度是 15%,但高度是图像的真实高度。我不知道如何使它具有与宽度完全相同的高度。它是一个方形图像,所以我试着让它尊重它的比例,将 wrap_content 放在高度上并调整 ViewBounds=true 但它没有用。
问题 3: ImageView 没有居中,左对齐。我需要它居中。
如何使用 XML 而不是 Java 代码来实现这一点?