ImageButtonをコードで作成
前の項目 でXMLファイルを使ってImageButtonを設定できましたが、
コードで記述するとどうなるでしょうか、ダイナミックにボタンのサイズを変更したい場合はコードで書かないとXMLのレイアウトでは難しいでしょう。
同じようにボタンサイズを400dp x 267dpとして作成してみます。また、フラグで背景画像とするか画像ソースにするか切り替えてみます。
MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
//package package.your.name; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.TypedValue; import android.view.Gravity; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private boolean flg = true; private TextView textView; private String str1, str2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); boolean addSrc = true; // リニアレイアウトの設定 LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); int mParent = LinearLayout.LayoutParams.MATCH_PARENT; int wContent = LinearLayout.LayoutParams.WRAP_CONTENT; layout.setLayoutParams(new LinearLayout.LayoutParams( mParent, mParent)); // レイアウト中央寄せ layout.setGravity(Gravity.CENTER); // 背景色 layout.setBackgroundColor(Color.parseColor("#ffdfcf")); setContentView(layout); // TextView の設定 textView = new TextView(this); str1 = "Image Button"; str2 = "Tapped"; textView.setText(str1); LinearLayout.LayoutParams textViewLayoutParams = new LinearLayout.LayoutParams( wContent, wContent); textView.setLayoutParams(textViewLayoutParams); // テキストカラー textView.setTextColor(Color.rgb(0xff, 0x0, 0x0)); // テキストサイズ textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 30); // margin int margin10 = 10 * TypedValue.COMPLEX_UNIT_DIP; textViewLayoutParams.setMargins(margin10, margin10, margin10, margin10); layout.addView(textView); // ボタンの設定 ImageButton imageButton = new ImageButton(this); int dp400 = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 400, getResources().getDisplayMetrics()); int dp267 = (int) TypedValue.applyDimension( TypedValue.COMPLEX_UNIT_DIP, 267, getResources().getDisplayMetrics()); LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams( dp400, dp267); // margin buttonLayoutParams.setMargins(margin10, margin10, margin10, margin10); // ボタンに画像を設定 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image_large); Drawable drawable = new BitmapDrawable(getResources(), bitmap); if(addSrc){ // Bitmapで画像を設定 imageButton.setImageBitmap(bitmap); // Drawableで画像設定 //imageButton.setImageDrawable(drawable); imageButton.setScaleType(ImageView.ScaleType.CENTER_INSIDE); imageButton.setBackgroundColor(Color.parseColor("#00000000")); } else{ // 背景に画像を設定 imageButton.setBackground(drawable); } imageButton.setLayoutParams(buttonLayoutParams); layout.addView(imageButton); // リスナーをボタンに登録 // lambda imageButton.setOnClickListener( v -> { if (flg) { textView.setText(str2); flg = false; } else { textView.setText(str1); flg = true; } }); } } |
コードで記述したためレイアウトXMLはいりません。
また、画像が小さいものであればボタンをwrap_contentに設定できます。
1 2 3 4 5 |
int wContent = LinearLayout.LayoutParams.WRAP_CONTENT; ... //ボタンサイズをwrap_contentにする場合 LinearLayout.LayoutParams buttonLayoutParams = new LinearLayout.LayoutParams( wContent , wContent ); |
関連ページ:
- 簡単な Button アプリを作る
- ButtonをJavaコードだけで設定する
- カスタムボタンを作る
- ImageButton:Button, ImageButton に画像を設定する
- onClickListenerの色々な設定
- Button 配列を設定する