2016-11-26 11 views
0

私は主な活動をポピュレートするための動的なボタンを作成しました。 ボタンが正常に機能します。 しかし、イメージの背景を設定しようとしています。ボタンの背景画像のサイズを設定する(Javaコード)

私はすべてをテストしましたが、imgの結果はとても悪いです。

画像をボタンの大きさに合わせるには?

私はこれをテストしたが、私にerroreを与えるのgetWidth beacause動作していない> 0

 int newWhidt=button.getWidth(); 
     int newHeight=button.getHeight(); 
     Bitmap originalbitmap= BitmapFactory.decodeResource(getResources(),R.drawable.trasparent); 
     Bitmap scaleBitmap= Bitmap.createScaledBitmap(originalbitmap,newWhidt,newHeight,true); 
     Resources resource=getResources(); 
     button.setBackgroundDrawable(new BitmapDrawable(resource, scaleBitmap)); 

これは私のコード

private void creabottoni(){ 
    TableLayout table=(TableLayout) findViewById(R.id.tableforbuttons); 
for(int row=0; row < NUM_ROWS; row++){ 
    TableRow tableRow=new TableRow(this); 
    tableRow.setLayoutParams(new TableLayout.LayoutParams(
      TableLayout.LayoutParams.MATCH_PARENT, 
      TableLayout.LayoutParams.MATCH_PARENT, 
      1.0f)); 

    table.addView(tableRow); 


    for(int col=0;col < NUM_COL; col++){ 
     final int FINAL_COL=col; 
     final int FINAL_ROW=row; 
     //Drawable immagine= getResources().getDrawable(R.drawable.trasparent); 
     final int FINAL_ID=buttonid;// creo FINAL_ID PER PASSARE ID A ACTIVITY OPERATORE 
     Button button=new Button(this); 
     button.setLayoutParams(new TableRow.LayoutParams(
       TableRow.LayoutParams.MATCH_PARENT, 
       TableRow.LayoutParams.MATCH_PARENT, 
       1.0f)); 


     Cursor res =myDB.getnamebyidbutton(buttonid); 
     buttonid++; 
     res.moveToFirst(); 


     //**********METTO TESTO BOTTONI FUNZIONA RES DBHELPER**************************************************** 
     button.setText(res.getString(res.getColumnIndex("NOME"))); 




     //qui controllo quale bottone ho premuto 

     button.setOnClickListener(new View.OnClickListener(){ 

      public void onClick(View v) { 
       bottonepremuto(FINAL_ID); 


      } 
     }); 



    }//fine ciclo colonna 
}//fine ciclo riga 

}//fine creabottoni 

答えて

0

オープンで、プロジェクトのディスプレイの各種の画像パッケージでプロジェクトビューすなわち:hdmi、xhdmi、xxhdmiなど。 xmlに同じ名前の同じサイズの同じ画像を置くだけで、それはdrawable/"画像の名前"として参照されます。

0

あなたは9パッチバックグラウンドをうまく使います。 そして

button.setBackground(your 9-patch);

tutorial for 9-Patch

0

のようなあなたのjavaファイル内の背景を設定し、あなたがクリック可能を与えたい場合は、私が代わりにボタンでのImageButton使用をお勧めします。

あなたはのImageButtonを使用する場合は、私が代わりに画像のベクトルを推薦するグラフィックスのために画像リソース

imageView.setBackgroundResource(R.drawable.your_drawable); 

に設定するには、次のコードを使用することができます。

Vector Asset Studio Tutorial

Vector Asset Studio In Android Studio

関連する問題