无法在LinearLayout中居中PercentRelativeLayout

时间:2018-05-23 20:12:30

标签: java android xml frontend

我有一个Fragment,它有一个方形相机(宽度和高度都等于屏幕宽度),在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。

知道可能出现什么问题吗?感谢任何努力:)

1 个答案:

答案 0 :(得分:0)

这是您尝试使用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>

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

相关问题