只需使用RelativeLayout將兩個Drawable放入EditText。 要設置內填充,放置一個隱形drawableRight到的EditText:
/res/values/dimens.xml
<resources>
<dimen name="iconSize">32dp</dimen>
</resources>
/res/layout/my_layout.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/editText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="textAutoComplete"/>
<ImageButton
android:id="@+id/imageButton1"
android:layout_width="@dimen/iconSize"
android:layout_height="@dimen/iconSize"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_1"/>
<ImageButton
android:id="@+id/imageButton2"
android:layout_width="@dimen/iconSize"
android:layout_height="@dimen/iconSize"
android:layout_toLeftOf="@+id/imageButton1"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_2"/>
</RelativeLayout>
In your Activity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout);
EditText editText = (EditText) findViewById(R.id.editText);
int iconSize = (int) getResources().getDimension(R.dimen.iconSize)
Drawable drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_1);
drawable.setBounds(0, 0, iconSize * 2, 0); // that is the trick!
editText.setCompoundDrawables(null, null, drawable, null);
}
你需要使用relativeLayout。 – k0sh
使用relativeLayout並在你的圖像中設置一個在relativeLayout下面的行。 – Destro