简体   繁体   中英

Custom EditText background white color remove

I want to remove the background white color from the editText Search bar.I want the search bar seems floating above. Please anyone help... Here is the screenshot of搜索栏.

search_input_style.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="32dp" />
    <padding android:top="12dp" android:left="12dp"
        android:bottom="12dp" android:right="12dp" />
</shape>

EditText xml code

EDIT (Here is the full xml code)

<?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:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#d8ebb5"
    tools:context=".MainActivity">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/market_rv"
        android:layout_width="411dp"
        android:layout_height="645dp"
        android:layout_marginEnd="8dp"
        android:background="#d8ebb5"
        android:paddingTop="20dp"
        android:paddingBottom="20dp"
        app:layout_constraintBottom_toBottomOf="parent"

        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/search_input"
        tools:listitem="@layout/customlist_item_market" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:src="@drawable/ic_add_black_24dp"
        app:backgroundTint="#0979D3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.915"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.845" />

    <EditText
        android:id="@+id/search_input"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="16dp"
        android:background="@drawable/search_input_style"
        android:backgroundTint="#D8EBB5"
        android:drawableLeft="@drawable/ic_search_gray_24dp"

        android:elevation="10dp"


        android:hint="search"
        android:inputType="textPersonName"
        android:textColor="@color/content_text_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

Actually this is working properly on emulator but it is not working on actual phone. There is another problem on emulator that is the search bar and the list is overlapped which doesn't occur on actual phone you can see this above screenshot of phone.模拟器截图在这里 .

To elevate you can use. android:elevation="10dp" And set the background color of EditText parent view as android:background="#D8EBB5" If it doesn't work then provide your full xml code.

If I understood correctly, you need to change the background color of the EditText's parent to #D8EBB5.

You didn't provide the entire XML code, but just try setting android:background="#D8EBB5" to the first EditText's parent background.

Just apply the background color to your parent view. It is just an example:

<LinearLayout
    android:background="@color/...."
    ...>

    <EditText
        android:id="@+id/edittext"
        android:drawableLeft="@drawable/ic_add_24px"
        android:drawableTint="@color/...."
        android:hint="Search"
        />

</LinearLayout>

Then with the Material Components Library apply a MaterialShapeDrawable :

float radius = getResources().getDimension(R.dimen.default_corner_radius);
EditText editText = findViewById(R.id.edittext);
ShapeAppearanceModel shapeAppearanceModel = new ShapeAppearanceModel()
    .toBuilder()
    .setAllCorners(CornerFamily.ROUNDED,radius)
    .build();

MaterialShapeDrawable shapeDrawable = new MaterialShapeDrawable(shapeAppearanceModel);
shapeDrawable.setStroke(1.0f, ContextCompat.getColor(this,R.color....));
shapeDrawable.setFillColor(ContextCompat.getColorStateList(this,android.R.color.transparent));

ViewCompat.setBackground(editText,shapeDrawable);

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM