1 回答

TA贡献2051条经验 获得超10个赞
TL;DR 这是另一种方法 ConstraintLayout
,不是针对每种屏幕尺寸使用单一布局
您可以使用ConstraintLayout支持不同的屏幕尺寸:
ConstraintLayout 允许您创建具有平面视图层次结构(无嵌套视图组)的大型复杂布局。它与 RelativeLayout 相似,因为所有视图都是根据兄弟视图和父布局之间的关系进行布局,但它比 RelativeLayout 更灵活,并且更容易与 Android Studio 的布局编辑器一起使用。
您可以使用这些属性以百分比指定视图大小:
app:layout_constraintWidth_percent=".5" app:layout_constraintHeight_percent=".5"
例如,单个按钮的高度和宽度都等于屏幕的 50%:
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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="match_parent"> <Button android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintWidth_percent=".5" app:layout_constraintHeight_percent=".5"/> </androidx.constraintlayout.widget.ConstraintLayout>
它看起来像这样:
我可以推荐使用ConstraintLayout with guidelines和Chains来支持不同的屏幕尺寸(除了我上面提到的 -app:layout_constraintWidth_percent
和app:layout_constraintHeight_percent
)。
乍一看,这可能需要大量工作,有些人可能想知道这是否真的值得付出努力,但这就是为什么我相信 ConstraintLayout 是构建 UI 的正确方法:
它真的很用户友好。
ConstraintLayout 非常简单易学。
一旦你学会了它,你会发现你节省了大量的开发时间,因为制作你的 UI 非常简单。
约束布局旨在支持不同的屏幕尺寸,因此无需为每个屏幕尺寸构建布局(这也与之前的优势相关——节省开发时间)。
添加回答
举报