繁体   English   中英

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

[英]Android custom layout for search widget

我想在Android应用程序的操作栏中有一个搜索字段。 我可以轻松完成默认设计。 但我想为搜索栏添加附件的图像设计。 有什么办法可以对我的搜索字段进行自定义设计?
我想像左边的图像一样,当单击时,用简单的动画将其变成右边的图像。
非常感谢

在此处输入图片说明

您可以通过创建实现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);

}
}

布局如下:

<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