2016-11-25 21 views
2

我不知道是什麼問題。我正在創建簡單的電話簿,所以我的名字中有abonent,圖像和通話按鈕的名稱。而當我嘗試設置按鈕的參數layoutParams的寬度時,它不會改變任何內容,btn.setWidth();也不起作用。不能使按鈕更寬由Java代碼

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    final LinearLayout linear = (LinearLayout)findViewById(R.id.linear); 
     LinearLayout.LayoutParams sublparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     LinearLayout.LayoutParams childparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(200, 200); 
     linear.setOrientation(LinearLayout.VERTICAL); 
     for(int i = 0; i < 10 ; i++){ 
      LinearLayout sublinear = new LinearLayout(this); 
      Button btn = new Button(this); 
      ImageView img = new ImageView(this); 
      TextView txt = new TextView(this); 
      img.setImageResource(R.drawable.img); 
      txt.setText("Abonent " + (i+1)); 
      btn.setBackgroundColor(getColor(R.color.mygreen)); 
      btn.setText("+3800000"); 
      txt.setLayoutParams(childparams); 
      btn.setLayoutParams(imageparams); 
      img.setLayoutParams(imageparams); 
      linear.addView(txt); 
      sublinear.addView(img); 
      sublinear.addView(btn); 
      linear.addView(sublinear, sublparams); 
     } 
    } 
} 

result of programm, green vertical lines are buttons, that i cannot to make wider您的按鈕使用LayoutParams設置寬度和heigth,所以你需要在這裏改變這一屬性(LinearLayout.LayoutParams(widht, height))

回答

0

LinearLayout.LayoutParams imageparams = new LinearLayout.LayoutParams(500, 300); 

和你的按鈕的尺寸必須改變,因爲你正在設置這個值。

btn.setLayoutParams(imageparams); 
+0

謝謝你,但我做過之前,不幸的是它不工作( –