【问题标题】:Change edittext line color for single control?更改单个控件的edittext行颜色?
【发布时间】:2016-07-04 08:44:32
【问题描述】:

我有一个位于深色背景上的编辑文本。屏幕的设计需要一个带有空白行和白色文本的编辑文本,以便可见。我认为这将是一项简单的任务,但我遇到了一个问题。

我已经在 StackOverflow 上查看过这个问题,但所有答案基本上都指向改变整个应用程序中所有 edittext 控件的样式;这不是一个选项,因为这只会影响单个屏幕上的 2 个。

我尝试了以下,但线条和提示都不是白色的;相反,两者仍然是黑色的。但是,当我在控件中键入内容时,文本是白色的。

        <EditText
            android:id="@+id/text_login"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_login"
            android:textColorHint="#FFFFFF"
            android:imeOptions="actionNext"
            android:singleLine="true"
            android:textColor="#FFFFFF"
            android:drawableBottom="#FFFFFF"
            />

drawableBottom 和 textColorHint 都尝试将线条和提示文本着色为白色。

有人对我在这里遗漏的内容有任何建议吗?任何帮助,将不胜感激!

谢谢!

【问题讨论】:

    标签: android android-edittext


    【解决方案1】:

    如果你的目标是 API 21 及以上,你应该使用 backgroundTint 属性:

    <EditText
        android:id="@+id/text_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
    
        android:hint="@string/hint_login"
    
        android:textColor="@color/white"
        android:backgroundTint="@color/white"
    
        android:textColorHint="@android:color/darker_gray"
        android:textColorHighlight="@color/colorPrimaryDark2"
    
        android:imeOptions="actionNext"
        android:singleLine="true"
    
        />
    

    如果您的目标 API 级别低于 21,则应使用自定义可绘制对象来更改下划线。您可以轻松地从 this site 创建一个包含九个补丁的图像,然后将其用作视图的背景。

    【讨论】:

    • 这正是我所需要的!非常感谢=D
    【解决方案2】:

    假设您在 "black" background 上有一个 EditText。您可以根据自己的背景更改代码。

    创建一个 xml 文件,该文件将被设置为 EditText 的背景。 xml 文件说 bg.xml 看起来像 -

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:top="-2dp" android:left="-2dp" android:right="-2dp">
            <shape android:shape="rectangle">
                <stroke android:width="1dp" android:color="#ffffffff" />
                <solid android:color="@android:color/black" /> //Change this color according to your background
            </shape>
        </item>
    </layer-list>
    

    你的 EditText 应该是 -

    <EditText
                android:id="@+id/text_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_login"
                android:textColorHint="#FFFFFF"
                android:imeOptions="actionNext"
                android:singleLine="true"
                android:textColor="#FFFFFF"
                android:background="@drawable/bg"
                />
    

    【讨论】:

      【解决方案3】:

      这个问题有一个简单的解决方法。您可以使用具有透明背景和底部视图的 EditText。设置该视图的颜色,它看起来像一条线。

      Layout.xml

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@android:color/background_dark"
          android:orientation="vertical"
          android:padding="30dp">
      
      
          <EditText
              android:id="@+id/text_login"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@android:color/transparent"
              android:hint="Hint here"
              android:imeOptions="actionNext"
              android:singleLine="true"
              android:textColor="#FFFFFF"
              android:textColorHint="#FFFFFF" />
      
          <View
              android:id="@+id/editTextBottom"
              android:layout_width="match_parent"
              android:layout_height="1dp"
              android:background="@android:color/white" />
      </LinearLayout>
      

      快乐编码:)

      【讨论】:

        猜你喜欢
        • 2016-11-29
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多