Xamarin Native Android axml水平LinearLayout中TextView与Button左右对齐问题
问题原因
你当前水平LinearLayout内的TextView和Button均设置了android:layout_weight="1",两个控件会平分父布局的可用宽度,因此无法实现分别靠左右两侧对齐的效果。
解决方案(基于现有LinearLayout修改)
只需调整两个控件的宽度和权重配置即可,核心逻辑是让TextView占据所有剩余空间,Button仅占用自身内容所需宽度:
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/background_light"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/background_light" android:layout_marginBottom="15dp" android:minWidth="25dp" android:minHeight="25dp" android:gravity="center_vertical"> <TextView android:text="Global transposition:" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:layout_marginLeft="10dp" android:layout_marginRight="5dp" android:layout_marginBottom="5dp" android:textSize="20sp" android:textColor="@android:color/black" android:textStyle="normal" android:id="@+id/transp_glob" /> <Button android:text="Set" android:textSize="20sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginRight="10dp" android:layout_marginBottom="5dp" android:id="@+id/ButtonSetTranspGlob" /> </LinearLayout> </LinearLayout> </ScrollView>
可选优化方案
如果后续布局复杂度提升,更推荐使用ConstraintLayout实现对齐效果,性能和灵活性都优于多层嵌套的LinearLayout。
内容的提问来源于stack exchange,提问作者Kamil
相关产品推荐
相关产品推荐

