2011-11-10 18 views
0

私はしたいときに私のレイアウトに動的にボタンを追加したいです。 ボタンは、このXMLボタンのようにする必要があります:プログラムでボタンを作成するにはどうすればよいですか?

<Button android:text="Text" 
android:gravity="bottom" 
android:textSize="10dp" 
android:textColor="#FFFFFF" 
android:layout_width="wrap_content" 
android:background="@drawable/attack1" 
android:layout_height="wrap_content" 
android:id="@+id/workingButton"> 
</Button> 

public class GravityIssueActivity extends Activity 
{ 
    LinearLayout layout; 
    Button newButton; 
    Button buttonByXml; 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //the button in the xml file 
     buttonByXml = (Button)findViewById(R.id.workingButton); 
     layout = (LinearLayout)findViewById(R.id.layoutToInsert); 
     //my new programatically "born" button 
     newButton = new Button(this); 
     //Setting the properties as i want 
     newButton.setText("Text"); 
     newButton.setTextSize(10); 
     newButton.setTextColor(Color.WHITE); 
     newButton.setBackgroundResource(R.drawable.attack1); 
     // Gravity = Bottom !!!!!!!!!! 
     newButton.setGravity(Gravity.BOTTOM); 
     // getting the XML buttons params just for case... 
     newButton.setLayoutParams(new LayoutParams(buttonByXml.getLayoutParams())); 
     //Adding my new Button to the layout 
     layout.addView(newButton); 
    } 
} 

そして、ここでの結果の画像です:

enter image description here

私はすべての属性をコピーすることが可能である異なる結果となりました方法は?

+0

正直に言うと、私は重力のみで問題が発生しました。私はテキストが底になるようにすることはできません。 (LinePane.LayoutParams) params.gravity = 80はちょうどうまく機能していません。 –

+0

ボタンの内側にテキストを配置するか、親の中にボタンを配置するには重力が必要ですか? – Vladimir

+1

私はちょうどボタンのテキストをボタンの下部に揃えたいと思っています:) –

答えて

0

これはあなたの現在の親アクティビティやfragmnetビューに追加することができるビューインスタンスを与える

Button b = new Button(); 

を使用してみてください。可能な設定の詳細については、http://developer.android.com/reference/android/widget/Button.html

を参照してください。オブジェクト階層内の親ビューによって提供されるすべてのメソッドを使用できます。

0

ボタンの下にテキストを整列する必要がある場合は、あなたが必要とするすべてがある:あなたが作成したい場合はcode.you以下

Button button = ... 
//apply required paramteres 
button.setGravity(Gravity.BOTTOM); 
+0

このスニペットはボタンのボトムとテキストの間に空きスペースが残るため、何か問題があります。これはなぜ私がこの質問を投稿した...私はそれを得ることはできません。 –

+0

あなたはあなたが得ている結果を提供できますか?これを見ることなく、 'setLineSpacing()'を必要以上に大きく設定するか、ボタンのテキストを設定するために使用する文字列の最後に '\ n'を置いていると仮定できます。 – Vladimir

+0

私はsetLineSpacing()を使用していません。 私は上記のXMLコードを使用すると、テキストの下に素敵な画像ボタンがあります。 .setGravity(Gravity.BOTTOM)のようなコードで作成しようとすると、テキストとボタンの底面との間に空きスペースがあります。 –

0

使用はまた、他のパラメータ

Button submit=new Button(this); 
    LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 
    params.setMargins(25, 0, 25, 0); 
    submit.setLayoutParams(params); 
    submit.setText("Attack"); 
    submit.setTextSize(10); 
    submit.setTextColor(getResources().getColor(R.color.white)); 
    submit.setBackgroundResource(R.drawable.attack); 
+0

設定されたマージンは、異なる解像度のデバイスで同じ結果になりますか? –

1

を追加(Button、textviewなどのような)動的ビューは、このコードを使用してアプリケーションで実行します。

MyActivity.java://your javaファイル、XMLファイルに

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); 
Button btn = new Button(this) 
btn.setText("My Dynamic Button); 
btn.setMinLines(1); 
btn.setMaxLines(3); 
ll.addView(et); 

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_alignBottom="@+id/TextView01" 
android:layout_below="@+id/relativeLayout1" 
android:orientation="vertical" >