1

可以在 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 代码来实现这一点?

4

4 回答 4

1

这就是我在 XML 中的做法,这样我的图像高度等于屏幕宽度的 15% :

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/favorite"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_launcher_background"
        app:layout_constraintDimensionRatio="H, 100:15"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</android.support.constraint.ConstraintLayout>

看看结果:

结果

于 2018-02-28T21:51:05.497 回答
0

不,AFAIK 通过 XML 是不可能的。但是您可以通过编程轻松实现它:如果您愿意,您可以创建一个带有所需参数的自定义视图,然后将其放入 XML 中。查看https://stackoverflow.com/a/20427215/1159507以供参考。

于 2018-02-27T08:49:24.337 回答
0

如何让它占据屏幕宽度的 15%:

//Get 15% of screen width:
myWidth = getWindow().getDecorView().getWidth() * .15;

myImageView = findViewById(R.id.favorite);

myView.setWidth(myWidth);

//Height should be the same as the width
myView.setHeight(myWidth);
于 2018-02-27T08:50:21.147 回答
0

以下两行表示图像视图的宽度将以百分比确定,并且相对于父级。

app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.15"

以下行表示 imageview 的宽度和高度将相同。比例是宽:高。

app:layout_constraintDimensionRatio="1:1"

Guidelines您可以在约束布局下的视图中设置 % 宽度/高度。下面说该准则是其父级的 20%。然后,您可以放置​​约束设置为本指南的视图。

app:layout_constraintGuide_percent="0.2"

以下是您可以尝试的完整示例 xml。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#423dfe"

        app:layout_constraintWidth_default="percent"
        app:layout_constraintWidth_percent="0.15"
        app:layout_constraintDimensionRatio="1:1"

        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"

        app:layout_constraintBottom_toTopOf="@+id/guideline" />

</android.support.constraint.ConstraintLayout>

您只能使用 ConstraintLayout 的 beta 版本执行此操作:

compile 'com.android.support.constraint:constraint-layout:1.1.0-beta1'

相关 SO 帖子: 1 2 3

进一步阅读:约束布局

于 2018-02-27T10:18:23.323 回答