dar*_*ord 55 android scale aspect-ratio android-layout android-constraintlayout
考虑以下布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="match_parent">
<android.support.constraint.ConstraintLayout
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#0000FF"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintDimensionRatio="H,3:1"
tools:layout_editor_absoluteX="16dp" />
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
我不确定app:layout_constraintDimensionRatio是如何工作的.我的理解是比例总是宽度:高度.因此3:1将始终使ImageView看起来比高度宽3倍.前缀H或W告诉ConstraintLayout哪个维度应该尊重比率.如果是H则表示首先根据其他约束计算宽度,然后根据纵横比调整高度.然而,这是布局的结果:
高度是宽度的3倍,这是意料之外的.任何人都可以向我解释如何计算尺寸相对于app:layout_constraintDimensionRatio设置?
Eug*_*sov 83
您对方法的理解app:layout_constraintDimensionRatio是正确的.如果设置app:layout_constraintDimensionRatio="H,3:1"则表示首先根据其他约束计算宽度,然后根据纵横比调整高度.您的实现的唯一问题是您添加app:layout_constraintBottom_toBottomOf="parent"到ImageView,因此它app:layout_constraintDimensionRatio被忽略.
这是使用3:1宽高比调整ImageView大小的布局:
<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:background="#FF0000">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="#0000FF"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="H,3:1" />
</android.support.constraint.ConstraintLayout>
Run Code Online (Sandbox Code Playgroud)
这是结果视图:
Pha*_*inh 11
基本上,我们有
layout_constraintDimensionRatio(width:height)
Run Code Online (Sandbox Code Playgroud)
例
<!-- button which have width = it's content and height = 1/2 width -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent" <!-- I still think that we don't need this attribute but I when I don't add this, constraint not working -->
android:text="Button TEST RATIO 1"
app:layout_constraintDimensionRatio="2:1" />
Run Code Online (Sandbox Code Playgroud)
产量

<!-- button which have width = it's content and height = 1/2 width -->
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
android:text="Button TEST RATIO 2"
app:layout_constraintDimensionRatio="2" /> <!-- 2 here <=> 2:1 <=> 2/1 (1:1 <=> 1, 1/2 <=> 0.5, ....) ->
Run Code Online (Sandbox Code Playgroud)
产量

<!-- button which have width = match_parent and height = 1/2 width -->
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button TEST RATIO 3"
app:layout_constraintDimensionRatio="2" />
Run Code Online (Sandbox Code Playgroud)
产量

<!-- button which have width = match constraint and height = 1/2 width -->
<Button
android:layout_width="0dp"
android:layout_height="0dp"
android:text="Button TEST RATIO 4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintDimensionRatio="2" />
Run Code Online (Sandbox Code Playgroud)
产量

演示:https://github.com/PhanVanLinh/AndroidConstraintLayoutRatio
看看这些ImageView属性:
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
Run Code Online (Sandbox Code Playgroud)
这些性质覆盖layout_constraintDimensionRatio由于该ImageView被约束到所述底部,顶部和左侧导致主父View占据左,上和主屏幕的底部部分.
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
Run Code Online (Sandbox Code Playgroud)
这将是一个解决方案.layout_constraintBottom_toBottomOf如果希望视图显示在顶部,反之亦然,则可以省略.最好除去所有上述约束,除了layout_constraintDimensionRatio这是最推荐的解决方案.
| 归档时间: |
|
| 查看次数: |
40012 次 |
| 最近记录: |