1

カスタム背景、カスタム アイコン、およびそのアイコンの下のテキストを使用して ImageButton を作成したい

私がこれまでに持っているのは

<ImageButton
    android:id="@+id/imageButton1"
    android:layout_width="140dp"
    android:layout_height="120dp"
    android:layout_marginLeft="3dp"
    android:layout_marginBottom="6dp"
    android:background="@drawable/btn_rectangle_background"
    android:src="@drawable/ic_action_search"
/>

ただし、そこに置くandroid:text="blablabla"と表示されません:/

ic_action_homework は .PNG アイコンですが、btn_rectangle_background は形状を定義する XML ファイルです。

それが私が達成したいことです ここに画像の説明を入力

4

1 に答える 1

1

最初の答え:

ここのようなレイアウトの構造でなければなりません:

<LinearLayout
   android:widht_layout="80dp"
   android:height_layout="80dp"
   android:padding="10dp"
   android:gravity="center"
   android:layout_gravity="center"
   android:bacgkground="your_color ARGB"
   >
    <ImageView />
    <TextView />
</LinearLayout>

または2番目の答え:

カスタム ビューを作成する

public class customView extends View{
   public customView(Context context){
       super(context);
   }
   public customView(Context context, String s, Drawable d){
       super(context);

       // Set Width&Height for this view
       this.measure(80,80);
       // or layout params with specified height&width for this view
       Resources r = getResources();
       int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **Your width**,
            r.getDisplayMetrics());
       int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, **your height**,
            r.getDisplayMetrics());
       ViewGroup.LayoutParams pp = new ViewGroup.LayoutParams(width,height);
       this.setLayoutParams(pp);


       TextView _text = new TextView(context);
       ImageView _image = new ImageView(context);
       _text.setText(s);
       _image.setBackground(d);
       this.addView(_image);       
       this.addView(_text);

   }
   public customView(Context context, String s, Bitmap b){
     ....
     _image.setImageBitmap(b);
     ...
   }  
}

また、ビューをルート ビューに追加します #id=アクティビティからのレイアウトのコンテンツ:

findByView(R.id.content).addView(new customView((Context)this,"Your Text",getResources().getDrawable(R.drawable.icon));

またはパスによるパラメータービットマップを使用:

findByView(R.id.content).addView(new customView((Context)this,"Your Text",BitmapFactory.decodeFile("/sdcard/file.png"));
于 2014-05-31T14:59:36.007 に答える