【问题标题】:Android how to change text input background imageAndroid如何更改文本输入背景图片
【发布时间】:2019-01-12 23:15:38
【问题描述】:

我有以下输入文本的代码

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:background="@drawable/ic_login_screen_input_container">
        <EditText android:id="@+id/input_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:hint="Name"/>
    </android.support.design.widget.TextInputLayout>

添加android:background后,输入文字变成:

如您所见,我将圆形容器添加为背景图像,但输入文本框后面仍然有矩形框。我的最终目标是创建一个这样的输入文本框:

然后当用户开始在框中输入时,输入文本框应变为:

请告诉我应该如何实现这一目标

【问题讨论】:

    标签: android-layout material-design


    【解决方案1】:

    我通过启用 @Mohammad (https://stackoverflow.com/a/52401339/9793695) 建议的 Material Design 解决了这个问题,但是,我还需要按照 (Updating com.android.support libraries v7:26.+ to v7:28.0.0 throw Multiple dex files define Lcom/google/common/util/concurrent/ListenableFuture) 中的说明首先解决所有依赖项。希望这有帮助!

    【讨论】:

      【解决方案2】:

      使用 compileSdkVersion 28

      使用 targetSdkVersion 28

      依赖关系

      implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
      implementation 'com.android.support:design:28.0.0-alpha3'
      

      添加此代码

      <android.support.design.widget.TextInputLayout
              android:id="@+id/userIDTextInputLayout"
              style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <android.support.design.widget.TextInputEditText
                  android:id="@+id/userIDTextInputEditText"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:hint="Email or Username" />
          </android.support.design.widget.TextInputLayout>
      

      【讨论】:

        【解决方案3】:

        EditText 中添加android:background="@drawable/ic_login_screen_input_container" 并将其从TextInputLayout 中删除 add android:background="@null" 到您的EditText

        【讨论】:

        • 我尝试了你的建议,但不起作用,它的行为与我上面报告的相同
        • 我发现这行代码把它作为一种样式放在你的 textinputlayout 中,它会给你轮廓框样式 'style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"'
        • 无法解析,是否包含任何库?
        • 我试过了,但是遇到了合并 dex 错误:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex at sun.reflect.NativeConstructorAccessorImpl .newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java :423) 在 java.util.concurrent.ForkJoinTask.getThrowableException ...
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 2014-07-17
        • 2012-10-16
        • 2014-03-04
        • 2016-03-11
        相关资源
        最近更新 更多