【问题标题】:How can we overlay the icon with rectangular shape in android我们如何在android中用矩形覆盖图标
【发布时间】:2017-08-18 06:53:20
【问题描述】:

我想设计如下图所示的 UI。我们无法做到这一点。这是我的代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/EditShop"
        android:drawableLeft="@drawable/user_icon"
        android:background="@drawable/rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Shopname"
        android:inputType="text"
        android:padding="7dp"
        android:singleLine="true" />
</LinearLayout>

我想要这样的输出:

【问题讨论】:

  • 您的意思是编辑文本字段中的人形图标?

标签: android


【解决方案1】:

试试这个。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/EditShop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/rectangle"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="20dp"
        android:hint="Shopname"
        android:inputType="text"
        android:padding="7dp"
        android:singleLine="true" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/user_icon"
        android:layout_alignBottom="@+id/EditShop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

【讨论】:

    【解决方案2】:

    使用RelativeLayout 而不是LinearLayout。

    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <EditText
        android:id="@+id/EditShop"
        android:drawableLeft="@drawable/user_icon"
        android:background="@drawable/rectangle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:hint="Shopname"
        android:inputType="text"
        android:padding="7dp"
        android:singleLine="true" />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/user_icon"
        android:layout_alignBottom="@+id/EditShop"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    
    </RelativeLayout>
    

    【讨论】:

      【解决方案3】:

      您可以尝试使用图层列表。但我不确定它的正确缩放比例。 Understanding Android's <layer-list>

      防弹解决方案是在九个补丁中绘制您的背景。 https://developer.android.com/studio/write/draw9patch.html

      【讨论】:

        【解决方案4】:

        不用写代码&很好的方法,使用ContraintLayout,顺便这里是完整的布局文件&结果图片:

        调整 EditText 的大小并得到相同的结果:

        布局代码:

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout
                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:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@android:color/white" >
        
            <EditText android:layout_width="315dp" android:layout_height="65dp"
                      android:id="@+id/editText" tools:layout_editor_absoluteY="131dp"
                      tools:layout_editor_absoluteX="37dp"/>
            <ImageView
                    android:layout_width="42dp"
                    android:paddingLeft="5dp"
                    android:layout_height="45dp" app:srcCompat="@android:drawable/ic_lock_lock"
                    android:id="@+id/imageView2" android:layout_marginTop="8dp"
                    app:layout_constraintTop_toBottomOf="@+id/editText" app:layout_constraintBottom_toTopOf="@+id/editText"
                    app:layout_constraintVertical_bias="0.556"
                    app:layout_constraintEnd_toStartOf="@+id/editText" android:layout_marginEnd="8dp"
                    android:layout_marginRight="8dp" app:layout_constraintStart_toStartOf="@+id/editText"
                    app:layout_constraintHorizontal_bias="0.34"/>
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

        • 用你的可绘制图标更改 app:srcCompat="@android:drawable/ic_lock_lock" 或使用 android:src="@drawable/resourcename" 作为 png 资源
        猜你喜欢
        • 1970-01-01
        • 2020-07-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-03
        • 2019-02-26
        • 2023-03-09
        • 1970-01-01
        相关资源
        最近更新 更多