Android 在安卓系统中,有没有一种方法可以使元素居中,同时强制它保持在屏幕上?

Android 在安卓系统中,有没有一种方法可以使元素居中,同时强制它保持在屏幕上?,android,android-constraintlayout,Android,Android Constraintlayout,为了简单起见,假设我有两个文本视图A和B。A是一个固定大小,在它和父视图之间有一个固定的边距。B是可变尺寸。 目标是在合适的情况下将B定位在A下方的中心位置,但不让B离开屏幕 以下是我目前为两个元素获得的xml: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/ap

为了简单起见,假设我有两个文本视图A和B。A是一个固定大小,在它和父视图之间有一个固定的边距。B是可变尺寸。 目标是在合适的情况下将B定位在A下方的中心位置,但不让B离开屏幕

以下是我目前为两个元素获得的xml:

<?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"
    tools:context=".MainActivity">

  <TextView
      android:id="@+id/element_A"
      android:layout_width="48dp"
      android:layout_height="48dp"
      android:layout_marginTop="12dp"
      android:layout_marginEnd="12dp"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintTop_toTopOf="parent"
      android:text="elem_A"/>
  <TextView
      android:id="@+id/element_B"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:layout_constraintStart_toStartOf="@id/element_A"
      app:layout_constraintEnd_toEndOf="@id/element_A"
      app:layout_constraintTop_toBottomOf="@id/element_A"
      android:text="sml"/>

</androidx.constraintlayout.widget.ConstraintLayout>

下图显示了三幅图:左边是元素B足够小(大)时我看到的,中间是元素B太大(不好)时我看到的,右边是元素B太大时我想看到的

事实证明,从ConstraintLayout切换到LinearLayout实际上解决了这个问题。我仍然很好奇使用ConstraintLayout是否可以实现这一点,但现在我只使用LinearLayout

<?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="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginTop="12dp"
    android:layout_marginEnd="12dp"
    android:layout_gravity="end"
    tools:context=".MainActivity">

  <TextView
      android:id="@+id/element_A"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="end"
      android:gravity="end"
      android:layout_weight="1"
      android:text="elem_A"/>
  <TextView
      android:id="@+id/element_B"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:gravity="center"
      android:layout_weight="1"
      android:text="much much longer element B"/>
</LinearLayout>

我已更改了您的代码,请将所有代码粘贴到您的xml文件中

<?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"
tools:context=".MainActivity">

<TextView
    android:id="@+id/element_A"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginTop="32dp"
    android:text="elem_A"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="@+id/element_B"
    app:layout_constraintStart_toStartOf="@+id/element_B"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/element_B"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:text="sml"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="parent"
    tools:layout_editor_absoluteY="88dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

我已更改了您的代码,请将所有代码粘贴到您的xml文件中

<?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"
tools:context=".MainActivity">

<TextView
    android:id="@+id/element_A"
    android:layout_width="48dp"
    android:layout_height="48dp"
    android:layout_marginTop="32dp"
    android:text="elem_A"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="@+id/element_B"
    app:layout_constraintStart_toStartOf="@+id/element_B"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/element_B"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="32dp"
    android:text="sml"
    android:textAlignment="center"
    app:layout_constraintEnd_toEndOf="parent"
    tools:layout_editor_absoluteY="88dp" />

</androidx.constraintlayout.widget.ConstraintLayout>


首先创建LinearLayout并将方向设置为水平,然后再次需要创建子LinearLayout并将两个文本字段包装到其中,并将其布局设置为右侧,同时将方向设置为垂直。之后,您需要将LinearLayout的权重平均分配到两个文本字段中,并将两个文本字段的重心和布局重心设置为中心。提示:-不要使用硬编码值,而是对所有元素使用匹配父元素/换行内容。

首先创建LinearLayout并将方向设置为水平,然后再次需要创建子LinearLayout并将两个文本字段换行,并将其布局设置为右侧重力也将方向设置为垂直。之后,您需要将LinearLayout的权重平均分配到两个文本字段中,并将两个文本字段的重心和布局重心设置为中心。提示:-不要使用硬编码值,而是对所有元素使用match\u parent/wrap\u content。

说明:我不想在B太大而无法居中时移动A,我希望B基本上从app:layout\u constraintEnd\u toEndOf=“@id/element\u A”切换到app:layout\u constraintEnd\u toEndOf=“parent”澄清:我不想在B太大而无法居中时移动A,我希望B基本上从app:layout\u constraintentend\u-toEndOf=“@id/element\u A”切换到app:layout\u constraintEnd\u-toEndOf=“parent”也许我有点误解,但我想这就是我目前使用app:layout\u constraintStart\u-toEndOf=“@id/element\u-A”所做的app:layout\u constraintentend\u toEndOf=“@id/element\u A”问题是,如果元素B太大,它会从屏幕右侧消失也许我误解了,但我认为这就是我目前使用app:layout\u constraintStart\u toStartOf=“@id/element\u A”app:layou\u constraintEnd\u toEndOf=“@id/element\u A”所做的事情问题是,如果元素B太大,它会从屏幕上向右移动,当元素B太大而无法放在它下面时,它不会将元素A向左移动吗?我试图有效地放弃在B太大而无法容纳时将B居中,并将A保持在同一位置。不,如果在文本字段中正确分配LinearLayout的权重,它不会移动。在元素B的文本字段中,您必须使用换行符内容作为高度,并设置宽度以匹配父元素。当文本变大时,它将自动换行。可能我误解了您的评论,但我得到了一个更奇怪的视图,当元素B足够小,可以容纳并且元素B未居中时,元素A在左侧被截断在它下面。我将用我正在尝试的新xml更新我的问题。我稍微修改了一下,去掉了外线布局,并利用了引力,它成功了!感谢您建议切换到linearlayout!当元件B太大而无法安装在其下时,该元件是否会将元件A向左移动?我试图有效地放弃在B太大而无法容纳时将B居中,并将A保持在同一位置。不,如果在文本字段中正确分配LinearLayout的权重,它不会移动。在元素B的文本字段中,您必须使用换行符内容作为高度,并设置宽度以匹配父元素。当文本变大时,它将自动换行。可能我误解了您的评论,但我得到了一个更奇怪的视图,当元素B足够小,可以容纳并且元素B未居中时,元素A在左侧被截断在它下面。我将用我正在尝试的新xml更新我的问题。我稍微修改了一下,去掉了外线布局,并利用了引力,它成功了!感谢您建议切换到linearlayout!