EditText と ImageView を含む線形レイアウトがあります。EditText に @null 背景を指定し、LinearLayout に次の背景を指定しました。
?android:attr/editTextBackground
全体が 1 つのウィジェットのように見えるようにします。EditText がフォーカスを取得する/選択されると、線形レイアウトの背景のドローアブルを更新して、全体が選択されていることを示したいと思います。
Linear Layout の私のレイアウト XML:
<LinearLayout
android:id="@+id/search_plate"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/editTextBackground">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:layout_weight="1"
android:background="@null"
android:height="36dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/image_view_close"
android:src="@drawable/ic_clear"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"/>
</LinearLayout>
これは、EditText がフォーカスされているときに LinearLayout の背景状態を変更しようとするために使用しているコードです。
public class IconEditText extends LinearLayout implements View.OnFocusChangeListener {
private static final String LOG_TAG = "IconEditText";
private View mSearchPlate; // Linear Layout
private EditText mEditTextSearch;
public IconEditText(Context context) {
this(context, null);
}
public IconEditText(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.icon_edit_text, this, true);
mSearchPlate = findViewById(R.id.search_plate);
mEditTextSearch = (EditText) findViewById(R.id.editText);
mEditTextSearch.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View view, boolean focused) {
mSearchPlate.getBackground().setState(focused ? FOCUSED_STATE_SET : EMPTY_STATE_SET);
}
}
FOCUSED_STATE_SET を使用するだけでなく、次のことも試しました。
ENABLED_FOCUSED_SELECTED_STATE_SET
FOCUSED_SELECTED_STATE_SET
ENABLED_SELECTED_STATE_SET
SELECTED_STATE_SET
上記のいずれも LinearLayout の背景を青い下線に変更していないようです。どんな助けでも大歓迎です!