Android开发:如何通过代码实现Tag系列按钮的指定布局?
解决Android非规整布局的实现方案
Hey! 刚接触Android布局就碰到这种非规整的需求,确实容易卡壳——毕竟LinearLayout的权重方案更适合那种统一表格式的布局,遇到这种需要精准定位的场景就不太顺手了。不过别担心,用RelativeLayout或者官方推荐的ConstraintLayout就能轻松搞定你的需求!
需求回顾
你需要实现:
- Tag5 位于 Tag0 的右侧,同时在 Tag1 的下方
- Tag6 和 Tag7 紧邻 Tag5 布局
方案1:使用RelativeLayout实现
RelativeLayout通过控件间的相对位置关系来布局,刚好匹配你的定位需求:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 基础控件Tag0 --> <TextView android:id="@+id/tag0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag0" android:padding="8dp"/> <!-- Tag1在Tag0下方 --> <TextView android:id="@+id/tag1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag1" android:padding="8dp" android:layout_below="@id/tag0"/> <!-- Tag5同时依赖Tag0右侧和Tag1下方 --> <TextView android:id="@+id/tag5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag5" android:padding="8dp" android:layout_toRightOf="@id/tag0" android:layout_below="@id/tag1"/> <!-- Tag6在Tag5下方 --> <TextView android:id="@+id/tag6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag6" android:padding="8dp" android:layout_below="@id/tag5"/> <!-- Tag7紧邻Tag6右侧,和Tag6顶部对齐 --> <TextView android:id="@+id/tag7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag7" android:padding="8dp" android:layout_toRightOf="@id/tag6" android:layout_alignTop="@id/tag6"/> </RelativeLayout>
方案2:使用ConstraintLayout实现(推荐)
ConstraintLayout是Android官方主推的布局控件,性能更优,能应对更复杂的布局场景,通过约束关系来精确定位控件:
<androidx.constraintlayout.widget.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"> <!-- 基础控件Tag0 --> <TextView android:id="@+id/tag0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag0" android:padding="8dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> <!-- Tag1约束在Tag0下方 --> <TextView android:id="@+id/tag1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag1" android:padding="8dp" app:layout_constraintStart_toStartOf="@id/tag0" app:layout_constraintTop_toBottomOf="@id/tag0"/> <!-- Tag5约束在Tag0右侧、Tag1下方 --> <TextView android:id="@+id/tag5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag5" android:padding="8dp" app:layout_constraintStart_toEndOf="@id/tag0" app:layout_constraintTop_toBottomOf="@id/tag1"/> <!-- Tag6约束在Tag5下方 --> <TextView android:id="@+id/tag6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag6" android:padding="8dp" app:layout_constraintStart_toStartOf="@id/tag5" app:layout_constraintTop_toBottomOf="@id/tag5"/> <!-- Tag7约束在Tag6右侧,和Tag6顶部对齐 --> <TextView android:id="@+id/tag7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tag7" android:padding="8dp" app:layout_constraintStart_toEndOf="@id/tag6" app:layout_constraintTop_toTopOf="@id/tag6"/> </RelativeLayout>
额外提示
你提到的屏幕尺寸获取代码(Display display = getWindowManager().getDefaultDisplay(); Point size = new Po...),其实完全不需要手动计算布局位置——上面的布局控件已经帮你处理了适配逻辑,Android布局系统会自动根据不同屏幕尺寸调整控件位置,省去手动计算的麻烦。
内容的提问来源于stack exchange,提问作者Noreen
相关产品推荐
相关产品推荐

