android-如何将此LinearLayout和RelativeLayout转换为ConstraintLayout?

android-如何将此LinearLayout和RelativeLayout转换为ConstraintLayout?,第1张

概述我正在尝试开始使用ConstrainLayout,但是发现它很难使用.我有一个用于设置屏幕的布局,它使用LinearLayout和RelativeLayout,非常简单且易于实现.这是布局:<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"

我正在尝试开始使用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?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1091861.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存