【问题标题】:Android custom layout for search widget用于搜索小部件的 Android 自定义布局
【发布时间】:2015-02-18 17:46:00
【问题描述】:

我想在我的 Android 应用的操作栏中有一个搜索字段。我可以使用默认设计毫无困难地做到这一点。但我想为我的搜索栏附加图像设计。有什么方法可以为我的搜索字段进行自定义设计?
我想像左图一样,点击后,通过简单的动画变成右图。
非常感谢

【问题讨论】:

  • 请发布定制设计
  • @ZygoteInit 哎呀,对不起。我附上了图片
  • 我过去曾使用自定义布局制作ActionBars,但从来没有使用自定义搜索字段。我得考虑一下。

标签: android search android-actionbar


【解决方案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>

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多