在约束布局中,如果我希望图像水平居中并位于屏幕顶部,那么我可以这样做
<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/