- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试开始使用 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>
如您所见,Switch 按钮未与 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呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47319882/
我有一个 LinearLayout,上面有几个 View 。所以我想要实现的是复制这个 LinearLayout 并在某个时候使用它。但是,当我对原始 LinearLayout 执行更改(例如方法 r
我试着做一个像这样的简单布局 仅显示第一个 TextView(“hello”)。我做错了什么? 最佳答案 制作内部布局wrap_content的l
我想构建某种 Twitter 应用程序。在 DashboardActivity 中,我需要在每次单击“发布”按钮时添加一个状态框。我的仪表板 xml 如下所示: -->header
我试图在另一个 LinearLayout 中插入一个 LinearLayout。我不知道我的做法对不对。 我需要在不使用通货膨胀的情况下尝试这种方式。 LinearLayout addre
我的 Android UI 中只有一个 ImageView 和一个 TextView。目前 TextView 与 ImageView 在同一个 LinearLayout 中,因此文本有时会跑到 2 行
我正在尝试让“播放”图标的图像显示在我在 LinearLayout 中的列表项的右侧。 但是,由于某种原因,图像出现在
我有两个 LinearLayout 结构如下。 问题是第二个 LinearLayout (id=goi) 没有按预期显示。我尝试将顶部布局更改为 Relativ
我以编程方式创建了很多 LinearLayout 并将它们放在另一个 LinearLayout 上: public void loadList(){ LinearLayout linearLa
我想创建按钮 1 到 9,并且我想循环执行此操作。但在每 3 个计数中,我想创建一个新的 LinearLayout。 final LinearLayout[] ll2 = new LinearLa
我需要创建 X 个 linearLayout - 每个 linearLayout 包含文本(项目名称)、位图(项目位图图像)。 因此,我创建了一些 for 循环来创建动态 linearLayout 和
您好,我是 Android 开发的初学者。我制作了一个 LinearLayout 以美观的方式显示不同的信息。我想要一个带有 ScrollView 的屏幕,我可以在其中添加任意数量的 LinearLa
我是 Android 编程的新手,我正在尝试设置一个包含四个组件的 LinearLayout。 目前看起来是这样的: XML:
首先,这是我的 activity_main.xml 文件的结构: 相对布局 线性布局 fragment (谷歌地图) 线性布局 搜索栏 TextView 图片按钮 我试图将第二个线性布局(不包含 ma
我正在创建一个自定义复合布局,它由一个可点击的水平 LinearLayout 组成,其中包含一个 ImageView 和两个 TextView。最终,我希望能够使用单个字段引用整个内容,并根据用户 A
我在 main.xml 中有一个 LinearLayout: 我制作了另一个 XML 文件,名为 item_box.xml: 基本上,我想通过代码(以编程方式
我有一个 LinearLayout,它应该包含一个项目列表(每个项目都是一个 LinearLayout)。 问题是子项目 (LinearLayouts) 没有显示在另一个下面:只有第一个是可见的。 这
下午好 我正在尝试像这样进行倒计时: 为此,我制作了两个水平的 LinearLayout,一个用于数值,另一个放置在下方,用于标签,例如“DAYS”或“HOURS”。 我的目标是在数值中间对齐标签,如
我得到了这个应用程序(顺便说一句,它和完成的一样好),我需要在其中动态地将 LinearLayout View 添加到 LinearLayout 容器。它的工作方式是用户在 EditText 中写一些
在 Udacity Android 初学者类(class)之后,我想向 list_item View 添加一个音频图标按钮。现在 list_item.xml 有一个父级水平线性布局和一个嵌套的垂直线性
我得到了一个相对布局,其中包含另一个我用来替换“标题”的相对布局,一个稍后用作“控制面板”的线性布局,以及一个 horizontalScrollView,其中 horizontalScroll
我是一名优秀的程序员,十分优秀!