我正在尝试开始使用ConstrainLayout,但是发现它很难使用.我有一个用于设置屏幕的布局,它使用linearLayout和relativeLayout,非常简单且易于实现.这是布局:
<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical"> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/tv_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Stay awake" androID:padding="10dp"/> <Switch androID:ID="@+ID/switch_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true"/> </relativeLayout> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/tv_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Enable Notification" androID:padding="10dp"/> <Switch androID:ID="@+ID/switch_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true"/> </relativeLayout> <relativeLayout androID:layout_wIDth="match_parent" androID:layout_height="wrap_content"> <TextVIEw androID:ID="@+ID/tv_location" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Enable location" androID:padding="10dp"/> <Switch androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentRight="true" androID:layout_centerVertical="true"/> </relativeLayout></linearLayout>
这是它的外观:
现在,我想构建相同的设置UI,但是要使用ConstrainLayout,这是该设置UI的ConstrainLayout.
<?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"> <TextVIEw androID:ID="@+ID/tv_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Stay awake"/> <Switch androID:ID="@+ID/switch_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintRight_toRightOf="parent" /> <TextVIEw androID:ID="@+ID/tv_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Enable Notification" app:layout_constrainttop_toBottomOf="@+ID/tv_stay_awake"/> <Switch androID:ID="@+ID/switch_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_toBottomOf="@+ID/switch_stay_awake"/> <TextVIEw androID:ID="@+ID/tv_location" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Enable location" app:layout_constrainttop_toBottomOf="@+ID/tv_notification"/> <Switch androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_toBottomOf="@+ID/switch_notification"/></androID.support.constraint.ConstraintLayout>
原来是这样的:
如您所见,“切换”按钮未与TextVIEw对齐.如何在同一行水平上将它们与TextVIEw对齐,使其外观与我使用linearyLayout和relativeLayout所做的相同?我知道可以将TextVIEw和Switch按钮放在每行的relativeLayout中,但是如果这样做,就没有理由使用ConstrainLayout.
解决方法:
你可以试试看.
在您的Switch XML代码中添加app:layout_constraintBaseline_toBaselineOf =“ @ ID / tv_notification”.
<androID.support.constraint.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"> <TextVIEw androID:ID="@+ID/tv_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Stay awake"/> <Switch androID:ID="@+ID/switch_stay_awake" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintBaseline_toBaselineOf="@+ID/tv_stay_awake" app:layout_constraintRight_toRightOf="parent"/> <TextVIEw androID:ID="@+ID/tv_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Enable Notification" app:layout_constrainttop_toBottomOf="@+ID/tv_stay_awake"/> <Switch androID:ID="@+ID/switch_notification" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintBaseline_toBaselineOf="@+ID/tv_notification" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_toBottomOf="@+ID/switch_stay_awake"/> <TextVIEw androID:ID="@+ID/tv_location" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:padding="10dp" androID:text="Enable location" app:layout_constrainttop_toBottomOf="@+ID/tv_notification"/> <Switch androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" app:layout_constraintBaseline_toBaselineOf="@+ID/tv_location" app:layout_constraintRight_toRightOf="parent" app:layout_constrainttop_toBottomOf="@+ID/switch_notification"/></androID.support.constraint.ConstraintLayout>
输出值
总结以上是内存溢出为你收集整理的android-如何将此LinearLayout和RelativeLayout转换为ConstraintLayout?全部内容,希望文章能够帮你解决android-如何将此LinearLayout和RelativeLayout转换为ConstraintLayout?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)