【问题标题】:Improper UI - android不正确的用户界面 - 安卓
【发布时间】:2020-11-17 18:12:31
【问题描述】:

我正在尝试构建如下图所示的 UI:
请同时检查:https://imgur.com/a/fXPRBez

我的代码:

<androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:layout_marginEnd="16dp"
                    android:background="@drawable/drawableborder"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                    <ImageView
                        android:id="@+id/iv_attachment"
                        android:layout_width="18dp"
                        android:layout_height="18dp"
                        android:layout_marginTop="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginBottom="8dp"
                        android:paddingStart="@dimen/padding_big"
                        android:paddingEnd="@dimen/padding_big"
                        android:scaleType="centerInside"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/frameLayout"
                        app:srcCompat="@drawable/ic_gallery" />

                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@+id/gv_attach_image"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="16dp"
                        android:layout_marginEnd="8dp"
                        android:layout_toStartOf="@+id/iv_attachment"
                        android:layout_toLeftOf="@+id/iv_attachment"
                        android:nestedScrollingEnabled="true"
                        android:numColumns="3"
                        android:visibility="gone"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/tell_us_more" />

                    <com.google.android.material.textfield.TextInputLayout
                        android:id="@+id/tell_us_more"
                        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:hint="@string/what_do_you_need_help"
                        app:layout_constraintTop_toTopOf="parent">

                        <com.google.android.material.textfield.TextInputEditText
                            android:id="@+id/tell_us_moreone"
                            style="@style/whathelpdoyouneedontextinput"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" />

                    </com.google.android.material.textfield.TextInputLayout>

                    <FrameLayout
                        android:id="@+id/frameLayout"
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:background="@color/dell_grey2"
                        app:layout_constraintTop_toBottomOf="@+id/gv_attach_image">

                    </FrameLayout>
                </androidx.constraintlayout.widget.ConstraintLayout>  

这会生成如下所示的 UI:

如您所见,这会在TextInputLayout 和边框之间产生一个小间隙。我该如何解决这个问题?

【问题讨论】:

  • 问题为什么降级了?
  • 我也有这个问题。点赞。
  • 谢谢,为 android 创建类似的 UI 有什么运气吗?
  • style/whathelpdoyouneedontextinput 呢?

标签: android xml android-layout user-interface


【解决方案1】:

你可以这样做:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="24dp"
    android:layout_marginEnd="16dp"
    android:orientation="vertical"
    >

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/tell_us_more"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:hint="@string/hint_tell_us"
        app:boxStrokeColor="@color/......"
        app:placeholderText="Required"
        app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.top">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/tell_us_moreone"   
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </com.google.android.material.textfield.TextInputLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/shape_rounded"
        android:gravity="end">

    <ImageView
        android:id="@+id/iv_attachment"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:scaleType="centerInside"
        app:srcCompat="@drawable/ic_menu_gallery" />

    </LinearLayout>


</LinearLayout>

您可以将 shapeAppearanceOverlay 应用到您的TextInputLayout 以获得不同的角:

<style name="ShapeAppearanceOverlay.top" parent="">
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
</style>

在包含图像的布局中,使用只有 3 个边的形状背景:

<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetTop="-2dp">
    <shape>
        <stroke android:width="1dp" android:color="@color/...." />
        <corners 
            android:bottomRightRadius="8dp"
            android:bottomLeftRadius="8dp"/>

        <solid android:color="#FFFFFF"/>
     </shape>
</inset>

最后,TextInputLayout 使用带有 app:placeholderText="Required" 的占位符和带有红色星号的浮动标签(提示)。
1.2.0-alpha05 开始,您可以格式化提示文本。

例如,你可以使用类似的东西:

<com.google.android.material.textfield.TextInputLayout
    android:hint="@string/hint_tell_us"
    ...>

与:

<string name="hint_tell_us"><font fgcolor='#FF0000'>*</font> Tell us more about the issue</string>

【讨论】:

    【解决方案2】:

    您的布局看起来不太好,但为了挽救您已经拥有的东西,请尝试:

    1. 删除android:id="@+id/tell_us_more"下的style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
    2. 在同一位置添加app:boxBackgroundMode="none" (android:id="@+id/tell_us_more")

    【讨论】:

      【解决方案3】:

      试试这个:

      xml:

         <androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginStart="16dp"
          android:layout_marginTop="24dp"
          android:layout_marginEnd="16dp"
          style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
          android:background="@drawable/border_grey">
      
          <com.google.android.material.textfield.TextInputLayout
              android:id="@+id/txt_more"
              android:layout_width="0dp"
              android:layout_height="match_parent"
              android:hint="Tell Us More About The Issue"
              android:layout_marginTop="8dp"
              bind:boxBackgroundColor="#FFFFFF"
              bind:layout_constraintEnd_toEndOf="parent"
              bind:layout_constraintStart_toStartOf="parent"
              bind:layout_constraintTop_toTopOf="parent"
              bind:boxBackgroundMode="none">
      
              <com.google.android.material.textfield.TextInputEditText
                  android:id="@+id/tell_us_moreone"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent" />
      
          </com.google.android.material.textfield.TextInputLayout>
      
          <View
              android:id="@+id/line"
              android:layout_width="match_parent"
              android:layout_height="2dp"
              android:layout_marginTop="16dp"
              android:background="@color/grey"
              bind:layout_constraintEnd_toEndOf="parent"
              bind:layout_constraintStart_toStartOf="parent"
              bind:layout_constraintTop_toBottomOf="@+id/txt_more" />
      
      
          <androidx.appcompat.widget.AppCompatImageView
              android:id="@+id/iv_attachment"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_margin="16dp"
              android:scaleType="centerInside"
              bind:layout_constraintBottom_toBottomOf="parent"
              bind:layout_constraintEnd_toEndOf="parent"
              bind:layout_constraintTop_toBottomOf="@+id/line"
              bind:srcCompat="@drawable/ic_menu_gallery" />
      
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      可绘制形状:

      <shape xmlns:android="http://schemas.android.com/apk/res/android"
         >
      
      
      <stroke android:color="@color/grey" android:width="1dp"/>
      <solid android:color="@color/colorWhite"/>
      <corners android:radius="@dimen/radius_8dp"/>
      
      
      </shape>
      

      注意:您可以将 RecyclerView 或 ... 添加到 ui。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      相关资源
      最近更新 更多