如何动态调整Button尺寸?Android双按钮尺寸适配问题求助
我来帮你搞定这个按钮尺寸跑偏的问题!先看看问题出在哪:你给player2按钮同时加了android:layout_below="@+id/rl1"和android:layout_alignParentBottom="true"这两个约束,当屏幕尺寸变化时,这俩约束会打架——系统会优先满足「底部对齐」的要求,同时把按钮高度拉满剩余空间,导致它和上面的player1尺寸不一样。
下面给你几个实用的解决方案,按需选就行:
方案1:改用LinearLayout垂直布局(最推荐)
LinearLayout的垂直布局能直观分配空间,彻底避免RelativeLayout的约束冲突:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" android:orientation="vertical" tools:context=".Timer"> <Button android:id="@+id/player1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#696969" android:text="00:00" android:textSize="95sp" /> <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="100dp"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:layout_centerInParent="true" android:background="@android:color/transparent" android:scaleType="centerCrop" android:src="@drawable/ic_baseline_pause_circle_outline_24" /> </RelativeLayout> <Button android:id="@+id/player2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:background="#696969" android:text="00:00" android:textSize="95sp" /> </LinearLayout>
这里用layout_weight="1"让两个按钮平分「除去中间100dp布局外」的剩余空间,不管屏幕怎么变,俩按钮高度始终完全一致。
方案2:修复RelativeLayout的约束
如果你不想换布局,只需要删掉player2的android:layout_alignParentBottom="true"属性就行:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/black" tools:context=".Timer"> <Button android:id="@+id/player1" android:layout_width="match_parent" android:layout_height="300dp" android:background="#696969" android:text="00:00" android:textSize="95sp" /> <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="100dp" android:layout_below="@+id/player1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:layout_centerInParent="true" android:background="@android:color/transparent" android:scaleType="centerCrop" android:src="@drawable/ic_baseline_pause_circle_outline_24" /> </RelativeLayout> <Button android:id="@+id/player2" android:layout_width="match_parent" android:layout_height="300dp" android:layout_below="@+id/rl1" android:background="#696969" android:text="00:00" android:textSize="95sp" /> </RelativeLayout>
不过这个方案有个小缺点:如果屏幕高度小于300dp+100dp+300dp=700dp,布局会被裁剪,所以适配性不如LinearLayout。
方案3:使用ConstraintLayout(现代Android布局首选)
ConstraintLayout灵活性拉满,也能轻松实现等高度按钮:
<?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" android:background="@android:color/black" tools:context=".Timer"> <Button android:id="@+id/player1" android:layout_width="match_parent" android:layout_height="0dp" android:background="#696969" android:text="00:00" android:textSize="95sp" app:layout_constraintBottom_toTopOf="@+id/rl1" app:layout_constraintHeight_percent="0.4" app:layout_constraintTop_toTopOf="parent" /> <RelativeLayout android:id="@+id/rl1" android:layout_width="match_parent" android:layout_height="100dp" app:layout_constraintBottom_toTopOf="@+id/player2" app:layout_constraintTop_toBottomOf="@+id/player1"> <ImageButton android:layout_width="70dp" android:layout_height="70dp" android:layout_centerInParent="true" android:background="@android:color/transparent" android:scaleType="centerCrop" android:src="@drawable/ic_baseline_pause_circle_outline_24" /> </RelativeLayout> <Button android:id="@+id/player2" android:layout_width="match_parent" android:layout_height="0dp" android:background="#696969" android:text="00:00" android:textSize="95sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHeight_percent="0.4" app:layout_constraintTop_toBottomOf="@+id/rl1" /> </androidx.constraintlayout.widget.ConstraintLayout>
这里用layout_constraintHeight_percent把按钮高度设为父布局的40%,保证俩按钮高度完全一致,中间布局固定100dp,完美适配各种屏幕尺寸。
内容的提问来源于stack exchange,提问作者Hema Ashra
相关产品推荐
相关产品推荐

