2016-09-15 18 views
0

ボタンサイズに使用するすべての「最小幅」の複数のdimens.xmlファイルがあります。また、メインレイアウトとボタンレイアウト(マルチスクリーンサポート用)に複数のバージョンがあります。ボタンをLinearLayoutにプログラムで追加すると正しく動作しない

レイアウトにプログラムで複数のボタンを挿入し、このボタンをxmlからレイアウトして膨張させたいとします。

これは私がこれを行う方法です。

Button temp_button = null; 
    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for(int i = 0; i < 7; i++) { 
     temp_button = (Button)inflater.inflate(R.layout.answer_letter_button, null); 
     temp_button.setTextColor(accent_color); 

     answer_letters.add(temp_button); 

     answer_panel.addView(answer_letters.get(i)); 
} 

これは、これらのボタンの親のレイアウトです:

<LinearLayout 
     android:id="@+id/answer_panel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center" 
     app:layout_constraintBottom_toTopOf="@+id/letter_panel"> 
</LinearLayout> 

とボタンのレイアウト:私が見たいと思って何

<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="@dimen/answer_letter_button_size" 
    android:layout_height="@dimen/answer_letter_button_size" 
    android:text="M" 
    android:textStyle="bold" 
    android:textColor="@color/colorAccent" 
    android:textSize="@dimen/answer_letter_button_text_size" 
    android:layout_margin="@dimen/answer_letter_button_margin_size" 
    android:background="@drawable/shaped_button" /> 

enter image description here

私が実際に見るもの:

enter image description here

を私が間違って何をやっている、誰かが私に教えて?

申し訳ありませんが、間違って表現しました。 結果のボタンのサイズに何か問題があります。主にボタンの幅です。

+0

なぜ** answer_letters **の中にボタンビューを追加してから** answer_panel **を追加するのですか? –

+0

ここでループを実行しているので、そこにボタンの数が表示されています。 –

答えて

1

あなたはlayout_weightのparamを追加する必要がありますし、また、wrap_contentlayout_widthlayout_heightを変更するには、dimensから7つの項目や設定高さと幅を追加can'y 1に設定します。あなたのやり方はいつも間違っていて結果になるでしょう。 textSizeをdimensからのみ設定し、検索対象を探します。

<Button 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="M" 
     android:textStyle="bold" 
     android:textColor="@color/colorAccent" 
     android:textSize="@dimen/answer_letter_button_text_size" 
     android:layout_margin="@dimen/answer_letter_button_margin_size" 
     android:background="@drawable/shaped_button" /> 

android:weightSum="7"に親のレイアウトを追加します。

+0

申し訳ありませんが、間違って表現しました。つまり、結果として得られるボタンのサイズに何か問題があるということです。主にボタンの幅です。 –

+0

今すぐこのコードを試してください。 –

+0

ありがとう!できます。 –

関連する問題