简体   繁体   English

Android自定义布局的搜索小部件

[英]Android custom layout for search widget

I want to have a search field in my actionbar in my Android app. 我想在Android应用程序的操作栏中有一个搜索字段。 I can do it without difficulty with default design. 我可以轻松完成默认设计。 but I want to have the attached image design for my search bar. 但我想为搜索栏添加附件的图像设计。 Is there any way to have a custom design for my search field? 有什么办法可以对我的搜索字段进行自定义设计?
I want to to be like the left image and when clicked , turn into the right image with a simple animation. 我想像左边的图像一样,当单击时,用简单的动画将其变成右边的图像。
Thanks very much 非常感谢

在此处输入图片说明

You can create your custom EditText by creating a class implementing TextWatcher and create your own layout like: 您可以通过创建实现TextWatcher的类并创建自己的布局来创建自定义EditText,如下所示:

public class MyEditText extends LinearLayout implements TextWatcher {
private EditText editText;

public MyEditText(Context context) {
    super(context);
    initializeView(context, null);
}

...

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}

@Override
public void afterTextChanged(Editable s) {

}

public EditText getEditText() {
    return editText;
}


private void initializeView(Context context, AttributeSet attrs) {
    LayoutInflater inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.view_my_edittext, this, false);

    editText = (EditText) view.findViewById(android.R.id.edit);

}
}

With a layout like: 布局如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:focusable="false">

<EditText
    android:id="@android:id/edit"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="@dimen/drawable_size"
    android:layout_marginEnd="@dimen/drawable_size"
    android:background="@null"
    android:focusable="true" />

<TextView
    android:id="@android:id/hint"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableLeft="@drawable/logo"
    android:focusable="false" />

</FrameLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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