Android 如何在XML中为视图指定宽度和高度的百分比?

Android 如何在XML中为视图指定宽度和高度的百分比?,android,android-layout,android-xml,android-layout-weight,android-layoutparams,Android,Android Layout,Android Xml,Android Layout Weight,Android Layoutparams,可以在XML线性布局中添加一个ImageView,其宽度为屏幕宽度的15%,高度也完全相同 我尝试了以下对话框代码,当用户单击地图上的标记时,会显示该对话框代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="

可以在XML线性布局中添加一个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%,而不是屏幕宽度的15%,这是一个巨大的问题,因为对话框的宽度有时会根据其内容而有所增减

问题2:宽度为15%可以,但高度是图像的真实高度。我不知道如何使它的高度和宽度完全相同。这是一个正方形的图像,所以我试着让它尊重它的比例,将wrap_内容放在height和adjustViewBounds=true上,但它不起作用

问题3:图像视图没有居中,而是向左对齐。我需要它居中


如何用XML而不是Java代码实现这一点?

不,恐怕不可能通过XML实现。 但您可以通过编程轻松实现: 如果需要,您可以创建一个带有所需参数的自定义视图,并将其转换为XML。
查看以供参考。

如何使其占屏幕宽度的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);

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

app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.15"
下一行表示imageview的宽度和高度相同。比例是宽:高

app:layout_constraintDimensionRatio="1:1"
使用
指南
可以在约束布局下的视图中设置%宽度/高度。下面说的是该指南是其母公司的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>
相关SO员额:


进一步阅读:

我在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>


FYI@VishvaDave
PercentRelativeLayout
在API级别26.1.0中被弃用。@NileshRathod仅在目标版本为lower@NileshRathod你还有其他选择吗?张贴answer@NullPointerException这可能会对您有所帮助,当然,这也可以通过XML实现!看看这里:@ThomasMary如果我在
ConstraintLayout
中设置百分比值,它将是来自父布局的百分比,而不是屏幕的百分比,“不是吗?”我正在检查那个帖子,但是找不到如何设置宽度和高度。你能用你提出的解决方案发布一个ASNWER吗?我用XML而不是JavaI来询问你的答案,如果没有人用XMLNICE方法来解决这个问题,那你就不用知道指南只适用于正方形图像。ImageView本身占据所有屏幕宽度。
<?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>