简体   繁体   English

如何设置最大百分比以dp为单位的宽度?

[英]How to set a width in percent with maximum width in dp?

I have an ImageView that needs to stretch with screen width. 我有一个ImageView ,需要随着屏幕宽度进行拉伸。 But I do not want the view to stretch indefinitely. 但是我不希望这种观点无限期地延伸。 I also need to keep the ratio of the image to 16:9. 我还需要将图像的比例保持为16:9。 I am having a hard time to set a maximum width when using percentage in ConstraintLayout . ConstraintLayout使用百分比时,我很难设置最大宽度。 I found the ratio can be set using ConstraintLayout, but it is not required to use this layout as long as the ratio will be preserved (note that I cannot use a 3rd party library other than Glide). 我发现可以使用ConstraintLayout来设置比率,但是只要保留比率即可,就不需要使用此布局(请注意,除了Glide之外,我不能使用第三方库)。 Here is what I have now. 这就是我现在所拥有的。

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/list_item_image"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="16:9"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@id/list_item_title"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintWidth_percent="0.5"
        app:layout_constraintWidth_max="163dp"
        tools:src="@color/white" />

    <TextView
        android:id="@+id/list_item_title"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/list_item_image"
        app:layout_constraintTop_toTopOf="parent"
        tools:text="Test Title" />

</android.support.constraint.ConstraintLayout>

Without the app:layout_constraintWidth_max everything is working fine, but once that attribute is set, the percentage stops working at all and width is set to some arbitrary value (definitely not the 163dp I used). 没有app:layout_constraintWidth_max一切都可以正常工作,但是一旦设置了该属性,该百分比就完全停止工作,并且width设置为某​​个任意值(绝对不是我使用的163dp)。 But the fun part is that if the max width is higher than the half of the screen width, the percentage is working again. 但有趣的是,如果最大宽度大于屏幕宽度的一半,则该百分比将再次起作用。 It seems like those two attributes cannot work together, but I do not know how else to define what I need to do. 似乎这两个属性不能一起使用,但是我不知道该如何定义我需要做的事情。

You can do this without constraint layout you can use LinearLayout or RelativeLayout, Set Image Width to match parent and scale your image to 您可以在没有约束布局的情况下执行此操作,可以使用LinearLayout或RelativeLayout,设置“图像宽度”以匹配父对象并将图像缩放到

在此处输入图片说明

Link for details: https://thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide 链接以获取详细信息: https : //thoughtbot.com/blog/android-imageview-scaletype-a-visual-guide

Indeed, it seems like combining app:layout_constraintWidth_percent="0.5" and app:layout_constraintWidth_max together does not allow to produce the expected result. 确实,似乎将app:layout_constraintWidth_percent="0.5"app:layout_constraintWidth_max组合在一起无法产生预期的结果。 A workaround for this would be to use a Guideline to constrain the ImageView with the desired percentage. 一种解决方法是使用GuidelineImageView限制为所需的百分比。 You can then constrain the start of the TextView to the Guideline as well or to the end of the ImageView , depending on what you want: 然后,您可以根据需要将TextView的开始约束到GuidelineImageView的末尾:

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

    <ImageView
            android:id="@+id/list_item_image"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintDimensionRatio="16:9"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@id/guideline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintWidth_max="163dp"
            tools:src="@android:color/black" />

    <TextView
            android:id="@+id/list_item_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/list_item_image"
            app:layout_constraintTop_toTopOf="parent"
            tools:text="Test Title" />

</android.support.constraint.ConstraintLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM