首页 > 解决方案 > 如何在背景android附近使用删除按钮在背景内向左添加textview并向右添加edittext

问题描述

下面是我需要的图像。

在此处输入图像描述

你可以看到有蓝色背景,在里面,它有拖车号 1 是 textview 和 01010 是 editext (输入字段) 同样在背景之外,在蓝色背景附近有删除按钮。

我尝试的是,

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:background="@color/app_white">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

                    <RelativeLayout
                        android:id="@+id/relative_1"
                        android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/blue_light"
                android:layout_marginRight="60dp"
                android:layout_marginLeft="60dp">
                <TextViewÒ
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Trailer number"
                    android:padding="@dimen/margin_5"
                    android:layout_margin="5dp"
                    android:layout_alignParentLeft="true"/>
                <EditText
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:text="01010"
                    android:layout_alignParentRight="true"/>

            </RelativeLayout>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="0dp"
                android:layout_toRightOf="@+id/relative_1"
                android:background="@drawable/ic_delete_cion" />

        </RelativeLayout>
  </LinearLayout>
</LinearLayout>

但是删除图标根本不显示,并且行也在 01010 eddittext 中显示。

这是我的截图。

在此处输入图像描述

标签: androidxmlandroid-layout

解决方案


我对您的布局进行了一些更改,它符合您的要求并适合每种设备。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:orientation="horizontal">


    <LinearLayout
        android:id="@+id/relative_1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@color/default_blue_light">

        <TextView
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center|start"
            android:padding="10dp"
            android:text="Trailer number" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginStart="5dp"
            android:layout_marginEnd="5dp"
            android:background="@null"
            android:inputType="number"
            android:padding="5dp"
            android:text="01010"
            android:textSize="14sp" />



    </LinearLayout>

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@drawable/ic_delete_icon" />

</LinearLayout>


推荐阅读