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

时间:2015-02-18 17:46:00

标签: android search android-actionbar

我希望在我的Android应用中的操作栏中有一个搜索字段。我可以毫无困难地使用默认设计。但我想为我的搜索栏添加附加的图像设计。有没有办法为我的搜索字段设置自定义设计?
我希望像左边的图像一样,点击后,用简单的动画转到右边的图像 非常感谢

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以通过创建实现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>
相关问题