weightSumとlayout_weight
前回と同様に親weightSumを10、子のButtonを5と3に設定しました
package blog.test; import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //親LinearLayoutを作成 LinearLayout layout0 = new LinearLayout(this); layout0.setOrientation(LinearLayout.VERTICAL); LayoutParams p0 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); //setWeightSumを10に設定 layout0.setWeightSum(10); setContentView(layout0, p0); //ボタンを追加 Button button1 = new Button(this); button1.setHeight(0); button1.setText("Button1"); LinearLayout.LayoutParams lp1=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //weightを5に設定 lp1.weight=5; layout0.addView(button1,lp1); Button button2 = new Button(this); button2.setHeight(0); button2.setText("Button2"); LinearLayout.LayoutParams lp2=new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); //weightを3に設定 lp2.weight=3; layout0.addView(button2,lp2); } }注意すべき点はButtonのLayoutParamsが
LinearLayout.LayoutParamsに変更されている点です
LayoutParamsだと.weightでの設定ができません
スポンサードリンク