簡體   English   中英

如何將 ProgressBar 放在 EditText 內並將欄保持在其中?

[英]How to put ProgressBar inside of EditText and keep the bar right in it?

我想在 EditText 內有一個 ProgressBar,但我希望它就在其中,最好是在 EditText 的中心。 到目前為止,我遇到的所有解決方案都不允許酒吧在邊界內。 此外,我正在尋找盡可能干凈的解決方案,而不僅僅是在 ConstraintLayout 中放置超過 9000 個約束。

它應該是這樣的(圖片取自編輯文本中的android 進度條):

預期結果

但實際結果是這樣的:

實際結果

或者這個(如果我對酒吧使用較小的樣式):

實際結果

我的代碼是:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|center_vertical"/>

</FrameLayout>

當然,我可以只為高度設置一些硬編碼值,但這絕對是解決問題的壞方法。 此外,如果我將欄的樣式更改為較小的,這不是一個修復,因為我想對其進行一些控制(而且它看起來也不完美)。 我還計划添加諸如錯誤和成功圖標之類的東西,因此非常歡迎滿足此要求的解決方案,因此,如果您對如何將其正確放入輸入字段有任何線索。 我會很感激的。

提前謝謝各位!

您可以這樣實現,每當您想在 position 子布局中使用FrameLayout時,都不是一個好的布局。 考慮使用RelativeLayout或更好的ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="@null"
        android:hint="Phone number goes here" />

    <ProgressBar
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:padding="5dp" />

</RelativeLayout>

編碼快樂!!

你可以試試:

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

   <ProgressBar
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM