Android EditText 在其可绘制对象和文本之间绘制一条分隔线

标签 android android-edittext android-drawable

我有一个带有 drawableLeft 图像的 EditText。我需要在 drawableLeft 旁边的 EditText 中添加一条垂直线,如下例所示:

enter image description here

我能想到的最简单的方法是将该线添加到我的可绘制图像中。但是通常有没有其他方法可以做到这一点?

最佳答案

  1. 在res/drawable/shape.xml中创建一个矩形圆角

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke android:width="3dip" android:color="@android:color/darker_gray" />
    <corners android:radius="10dip"/>
    <padding android:left="10dip" android:top="10dip" android:right="10dip"        android:bottom="10dip" />
    </shape>
    
  2. 现在创建一个布局

    <?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="wrap_content"
    android:background="@drawable/shape"
    android:gravity="center_vertical">
    
    
    <ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/ic_launcher"/>
    
    <View
    android:layout_width="2dp"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"
    android:layout_marginLeft="10dp"/>
    
    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/editText"
    android:layout_weight="1"
    android:background="@null"
    android:layout_marginLeft="10dp"
    android:hint="Your bitcoin address here"/>
    
    
    </LinearLayout>
    
  3. 我已将线性布局背景设置为矩形圆角形状。它看起来和你的图片预览一模一样。

关于Android EditText 在其可绘制对象和文本之间绘制一条分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34541433/

相关文章:

android - Android中如何知道EditText发生了删除操作?

android - 如何使编辑字段android的固定大小

android - 数据绑定(bind)将android资源绑定(bind)到后台

android - WebView 本地文件在 Android 9.0 中不加载页面?

android - Activity 永远不会变为请求状态 "[DESTROYED, RESUMED, STARTED, CREATED]"(最后一个生命周期转换 = "PRE_ON_CREATE")

android - 看不到编辑文本

java - Android - 跟踪实时触摸事件以进行绘制

android - 如何在 xml 中重用不同颜色的已定义可绘制对象

java - Android 程序中包含未使用的 Swing 代码

android - Gradle errror错误: list 合并失败: