android - 使图像尺寸与屏幕尺寸成一定比例

标签 android android-layout

在约束布局中,如果我希望图像水平居中并位于屏幕顶部,那么我可以这样做

<android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/image"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            android:id="@+id/imageView" />
</android.support.constraint.ConstraintLayout>

如何让图片的宽度和高度为屏幕宽度和高度的25%?这可行吗?

谢谢

最佳答案

更新:

ConstrainLayout 1.1.0它已经实现了一个更好的方法来做到这一点!

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

    <ImageView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff0000"

        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintWidth_percent=".25"
        app:layout_constraintWidth_default="percent"

        app:layout_constraintHeight_percent=".25"
        app:layout_constraintHeight_default="percent"
        />

</android.support.constraint.ConstraintLayout>

或者

您可以放置​​带有百分比的 Guidelines,然后将 ImageView 约束到它们。 如果有一种更简洁的方法来做这件事会很好,但我认为它仍然比带权重的嵌套 LinearLayout 好得多

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

    <ImageView
        android:id="@+id/imageView"
        app:layout_constraintLeft_toLeftOf="@id/guideline_left"
        app:layout_constraintRight_toRightOf="@id/guideline_right"
        app:layout_constraintTop_toTopOf="@id/guideline_top"
        app:layout_constraintBottom_toBottomOf="@id/guideline_bottom"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#ffff0000"/>

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

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


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

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

</android.support.constraint.ConstraintLayout>

关于android - 使图像尺寸与屏幕尺寸成一定比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44340315/

相关文章:

android - 复选框首选项和检查是否启用或禁用

javascript - Frida - 访问具有所需类型的类属性

android - 源值 1.5 已过时,将在未来版本中删除

显示软输入时Android全屏调整大小

Android 界面兼容性问题

android - RecyclerView 项目超出边距范围

java - Android 数据库问题

java - ListView 中的新 Activity

android - 如何在选项卡式应用程序中替换 LinearLayout 下的 fragment

Android垂直进度条