2016-06-17 6 views
0

このように、私は活動をしましたし、私はLayoutInflaterでそれにプログラム的にいくつかのボタンを作成することができます:(ボタン)

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
Button newButton = (Button) inflater.inflate(R.layout.my_button, currentTableRow, false); 

とmy_button.xml:

<Button xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/newButton" 
android:layout_width="30dp" 
android:layout_height="30dp"</Button> 

正常に動作します。しかし、私の質問は、次のとおりです。my_button.xmlに幅と高さを渡す方法はありますか、そのビューを拡張するときに幅と高さを設定する方法はありますか?私はいつか "30dp"を設定したい、時には "45dp"などに設定することを意味します。事前におねがいします。

+0

あなたはnewButton.setHeight(int)を探していますか? newButton.setWidth(int)? –

+0

@ M.WaqasPervezはい、それを行うには何らかの方法があります。 – Beppe

答えて

1
float scale = context.getResources().getDisplayMetrics().density; 

button.getLayoutParams().height = 30 * scale; 
button.getLayoutParams().width = 30 * scale; 

.. LayoutParams(int width, int height)

は、幅と2番目の引数が高されている最初の引数を意味し、この

newButton .setLayoutParams (new LayoutParams(45, LayoutParams.WRAP_CONTENT) 

を試すことができますし、 「dp」の幅。

1

あなたは1つが、単純に高さを設定することができ、コードの上に使用して

+0

ありがとうございます。できます。 – Beppe