【问题标题】:Can't center PercentRelativeLayout in LinearLayout无法在 LinearLayout 中居中 PercentRelativeLayout
【发布时间】:2018-05-24 10:00:32
【问题描述】:

我有一个片段,它有一个方形相机(宽度和高度都等于屏幕宽度),在 PercentRelativeLayout 中表示为 FrameLayout。在这个摄像头下面是一个 ImageView,它在单击时拍摄照片。

我想将方形相机垂直居中(因为它已经覆盖了屏幕宽度),然后将 ImageView 在其下方的剩余空间中居中。这是我的 XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/llCameraFragment"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <FrameLayout
            android:id="@+id/tvCamera"
            app:layout_widthPercent="100%"
            app:layout_aspectRatio="100%" />
    </android.support.percent.PercentRelativeLayout>

    <LinearLayout
        android:id="@+id/llBelowCamera"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center">

    <ImageView
        android:id="@+id/ivCaptureImage"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/camerashutter" />
    </LinearLayout>

</LinearLayout>

问题在于相机视图 (PercentRelativeLayout) 总是从屏幕的最顶部开始。我尝试在 PercentRelativeLayout 中使用 layout_gravity 而不是 gravity,并且还尝试将其包裹在 LinearLayout 周围,并将重力设置为 center / center_vertical。

知道有什么问题吗?感谢任何努力:)

【问题讨论】:

  • 您能解释一下您要实现的布局吗?您在 percentLayout 内有一个 100% 宽度的 frameLayout
  • 我希望相机视图是一个填充屏幕宽度的正方形。为了让它成为一个正方形,我需要它的高度等于那个宽度。我正在使用 PercentRelativeLayout 来保存相机片段(FrameLayout)
  • 我认为在这种情况下你最好使用ConstraintLayout

标签: java android xml frontend


【解决方案1】:

这就是您尝试使用ConstraintLayout 实现的目标。如您所见,它更容易且更高效。

<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"
  android:orientation="vertical">

  <FrameLayout
    android:id="@+id/tvCamera"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintDimensionRatio="H, 1:1"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"/>

  <ImageView
    android:id="@+id/ivCaptureImage"
    android:layout_width="100dp"
    android:layout_height="100dp"
    app:layout_constraintTop_toBottomOf="@id/tvCamera"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    />
</android.support.constraint.ConstraintLayout>

如果您最终使用它以及是否需要更多帮助,请告诉我

【讨论】:

    猜你喜欢
    • 2014-08-13
    • 2020-07-17
    • 1970-01-01
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-31
    相关资源
    最近更新 更多