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

Android EditText draw a divider line between its drawable and its text

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

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

试试这个,你的问题解决了

在您的 .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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@drawable/customborder"
        android:gravity="center|left"
        android:minHeight="40dip"
        android:orientation="horizontal"
        android:padding="5dip"
        android:weightSum="2">

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:gravity="center"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/img_clear_to"
                android:layout_width="30dip"
                android:layout_height="30dip"
                android:src="@drawable/ic_action_delete" />
        </LinearLayout>

        <View
            android:id="@+id/vvv1"
            android:layout_width="2dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent" />

        <LinearLayout
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1.8"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_to"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clickable="true"
                android:padding="5dip"
                android:text="@string/text_to"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@color/black"
                android:textSize="@dimen/font_normal_size" />
        </LinearLayout>


    </LinearLayout>
</LinearLayout>

customborder.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners
        android:radius="2dp"
        android:topRightRadius="0dp"
        android:bottomRightRadius="0dp"
        android:bottomLeftRadius="0dp" />
    <stroke
        android:width="1dp"
        android:color="@android:color/black" />
</shape>

您可以创建一个 layout 并作为 属性

附加到 editText
android:drawableRight="@android:drawable/your_layout"

例如

<EditText 
   android:width="match_parent"
   android:drawableLeft="@android:drawable/your_layout"
/>

在你的 your_layout.xml 文件中你可以放任何你想要的东西。

请检查this

相当简单!

只需添加一个'FrameLayout'

 <FrameLayout
            android:id="@+id/vertical_divider"
            android:layout_width="2dp"
            android:layout_height="48dp"
            android:layout_toEndOf="@+id/<your_object>"
            android:layout_toRightOf="@+id/<your_object>"
            android:background="@color/grey" />

<your_object> 替换为您的可绘制图像。

  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. 我已将线性布局背景设置为矩形圆角形状。和urs的图片预览一模一样