簡體   English   中英

將視圖設置在相機預覽的底部?

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

我想將 TextView 的位置設置為始終位於相機預覽的底部。

我下面的布局不是這種情況。 當用戶更改相機預覽的縱橫比時(例如:從 16:9 到 4:3),TextView 保持在相同的位置,不再位於預覽的底部。

如何將 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>

抱歉,我沒有足夠的評論聲譽,所以我會嘗試直接回答

出現此問題是因為您在使用 ConstraintLayout 時使用相機預覽將 textview 從底部設置為底部。 我可以給你兩個選項來解決這個問題,

首先將 textview 從 app:layout_constraintBottom_toBottomOf 更改為 app:layout_constraintTop_toBottomOf。

第二次嘗試用線性布局包裹它並將方向設置為垂直。

嘗試這個:

<?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