ドローアブルの位置を android:drawableLeft から android:drawableRight/left/bottom に動的に変更するにはどうすればよいですか? setCompoundDrawablesWithIntrinsicBounds メソッドを使用する必要があることはわかっていますが、まず R.drawable を知る必要があります。* setCompoundDrawablesWithIntrinsicBounds メソッドで使用します。
private void changeLabelPosition(View view){
int drawableID =
if(getGravity = Gravity.LEFT){
view.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0, 0, 0);
}else if(getGravity = Gravity.TOP){
view.setCompoundDrawablesWithIntrinsicBounds(0, drawableID, 0 , 0);
}else if(getGravity = Gravity.RIGHT){
view.setCompoundDrawablesWithIntrinsicBounds(0, 0, drawableID, 0);
}else if(getGravity = Gravity.BOTTOM){
view.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, drawableID);
}
}
そのため、ビューのドローアブル リソースの ID が必要です。
この方法は適切ではありません。多くのボタンがあり、ハードコードを使用するのには適していません。
drawableID = 0;
switch (view.getId()){
case R.id.button_add:
drawableID = R.drawable.button_add;
case R.id.button_remove:
drawableID = R.drawable.button_remove;
...
...
...
}
if(getGravity = Gravity.LEFT){
view.setCompoundDrawablesWithIntrinsicBounds(drawableID, 0, 0, 0);
}
...
...
...
enter code here
編集
switch (mMainPanelGravity)
{
case Gravity.TOP:
for (View v : arrayOfViews)
{
Drawable[] drawables = ((TextView) v) .getCompoundDrawables();
if (drawables.length != 0) {
Drawable dr = drawables[0];
((TextView) v).setCompoundDrawablesWithIntrinsicBounds( null, dr, null, null);
}