Java Android:创建半透明的编辑文本,其中包含图像
我正在从事一个Android项目,在这个项目中,我有一个登录页面,我想在其中添加用户名和密码字段。我的设计要求我把它们放在半透明的地方,里面有一个图像。我能为EditText做些什么来改变它的半透明性和形状,有很多线程通过设置背景为空或颜色等来使它透明,但我发现没有这样的 设计: 我使用的是相对布局,右边的图标,我把它们放在单独的PNG文件中。我可以直接将它们添加到Edittext中,但我担心如果用户名和密码很长,它将开始覆盖图像。有什么策略吗?多谢各位 代码:Java Android:创建半透明的编辑文本,其中包含图像,java,android,android-edittext,Java,Android,Android Edittext,我正在从事一个Android项目,在这个项目中,我有一个登录页面,我想在其中添加用户名和密码字段。我的设计要求我把它们放在半透明的地方,里面有一个图像。我能为EditText做些什么来改变它的半透明性和形状,有很多线程通过设置背景为空或颜色等来使它透明,但我发现没有这样的 设计: 我使用的是相对布局,右边的图标,我把它们放在单独的PNG文件中。我可以直接将它们添加到Edittext中,但我担心如果用户名和密码很长,它将开始覆盖图像。有什么策略吗?多谢各位 代码: <?xml versio
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/background"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text"
android:id="@+id/textView"
android:textSize="32sp"
android:textColor="@color/common_signin_btn_dark_text_default"
android:layout_marginTop="75dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/textView"
android:layout_marginTop="41dp"
android:layout_alignRight="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_alignLeft="@+id/textView"
android:layout_alignStart="@+id/textView" />
</RelativeLayout>
更新截图
更新的编辑文本
layout_bg.xml:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/traslucent_background" />
<stroke android:width="1dip" android:color="#B1BCBE" />
<corners android:radius="17dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
试试这个:
如果您担心文本与可绘制文本重叠,可以在
EditText
中添加自定义的paddingRight
,以解决此问题。这将节省您膨胀自定义布局的工作量 对于图像,您应该能够使用android:drawableEnd=“@drawable/…”
和android:drawableRight=“@drawable/…”
,这也会限制文本长度
对于背景,您可以定义如下自定义可绘制图形:
<!-- background_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="@color/traslucent_background" />
</shape>
<color name="shadow_lighter_color">#64000000</color>
编辑:这是如何在文本和图标之间使用drawableEnd
/drawableRight
的示例:
<EditText
android:background="@drawable/background_edittext"
android:drawablePadding="16dp"
android:drawableEnd="@drawable/icon"
android:drawableRight="@drawable/icon"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
这工作很好。您能告诉我解决第二个问题的策略吗?右侧的用户名和密码图标无法写入文本。使用此属性(drawableEnd/right),您不必担心,文本将在图像之前停止:)(此外,您可以添加drawablePadding
以扩大文本和图标之间的gal)这是惊人的工作,只是一件事,图标看起来太大了,我怎么能调整它的位置和大小?恐怕你不能。该位置固定在编辑文本的末端
或右侧
,大小为可绘制大小。您必须生成一个具有所需大小的图标。我通常使用24dp。如果你还不知道,这是一个为所有屏幕配置生成自定义图标的非常棒的工具:图标是生成的,但它附着在最末端,位于边框顶部,添加屏幕截图,请2分钟。很高兴听到后者。不过,一定要让我知道您在我的代码中遇到了什么问题。会帮助我变得更好:)
<!-- background_edittext.xml -->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="8dp" />
<solid android:color="@color/traslucent_background" />
</shape>
<color name="shadow_lighter_color">#64000000</color>
<EditText
android:background="@drawable/background_edittext"
android:drawablePadding="16dp"
android:drawableEnd="@drawable/icon"
android:drawableRight="@drawable/icon"
android:maxLines="1"
android:ellipsize="end"
android:layout_width="match_parent"
android:layout_height="wrap_content" />