简体   繁体   English

将视图设置在相机预览的底部?

[英]Set view to be at bottom of camera preview?

I'd like to set the position of the TextView to always be at the bottom of the camera preview.我想将 TextView 的位置设置为始终位于相机预览的底部。

This is not the case with my layout below.我下面的布局不是这种情况。 When the user changes the aspect the ratio of the camera preview (ex: from 16:9 to 4:3), the TextView remains in the same position and is no longer at the bottom of the preview.当用户更改相机预览的纵横比时(例如:从 16:9 到 4:3),TextView 保持在相同的位置,不再位于预览的底部。

How do I set the position of the TextView to always be at the bottom (left) of the camera preview?如何将 TextView 的位置设置为始终位于相机预览的底部(左侧)?

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.activities.MainActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/root"
        android:background="@color/black"
        tools:context=".ui.activities.MainActivity">

        <FrameLayout
            android:id="@+id/main_frame"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:paddingTop="8dp">

            <androidx.constraintlayout.widget.ConstraintLayout
                android:id="@+id/preview_container"
                android:layout_height="match_parent"
                android:layout_width="match_parent">

                <androidx.camera.view.PreviewView
                    android:id="@+id/preview"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    app:layout_constraintDimensionRatio="H,9:16"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"

                    app:layout_constraintBottom_toBottomOf="@+id/preview"

                    android:textColor="#ffffffff"
                    android:text="Text Test" />

            </androidx.constraintlayout.widget.ConstraintLayout>
        </FrameLayout>
    </RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

Sorry i dont have enough reputation for comment so i will try answer directly抱歉,我没有足够的评论声誉,所以我会尝试直接回答

This problem occurs because you set your textview bottom to bottom with camera preview while use ConstraintLayout.出现此问题是因为您在使用 ConstraintLayout 时使用相机预览将 textview 从底部设置为底部。 I can give you two option for solve this,我可以给你两个选项来解决这个问题,

first change textview from app:layout_constraintBottom_toBottomOf to app:layout_constraintTop_toBottomOf.首先将 textview 从 app:layout_constraintBottom_toBottomOf 更改为 app:layout_constraintTop_toBottomOf。

Second try for wrap it with linearlayout and set orientation to vertical.第二次尝试用线性布局包裹它并将方向设置为垂直。

Try this:尝试这个:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.activities.MainActivity">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/root"
    android:background="@color/black"
    android:orientation="vertical"
    tools:context=".ui.activities.MainActivity">

    <FrameLayout
        android:id="@+id/main_frame"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingTop="8dp">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/preview_container"
            android:layout_height="match_parent"
            android:layout_width="match_parent">

            <androidx.camera.view.PreviewView
                android:id="@+id/preview"
                android:layout_width="0dp"
                android:layout_height="0dp"
                app:layout_constraintDimensionRatio="H,9:16"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:layout_gravity="bottom"

            android:textColor="#ffffffff"
            android:text="Text Test" />
    </FrameLayout>
   
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM