Android按钮宽度动态调整通过代码

时间:2016-04-25 09:58:07

标签: android android-layout android-studio

考虑如果我有3个按钮的情况,那么前50个布局中的前两个按钮应该并排,第三个按钮应该动态地在布局中留下剩余的50%宽度。如果有5个按钮,那么每行的布局将被划分为33.33%,其中第1,第2按钮和第3,第4按钮将并排,每个宽度为16%,最后,即第5个应该具有33.33%的宽度。

 <TableLayout android:id="@+id/tableLayout1" 
android:layout_width="match_parent"
 android:layout_height="300dp"
 android:layout_marginTop="200dp" 
android:padding="5dp"
 android:shrinkColumns=""
 android:stretchColumns="" > 
<Button android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="New Button" 
android:id="@+id/button" 
android:layout_marginTop="220dp"/>
 </TableLayout>






   mTlayout = (TableLayout) findViewById(R.id.tableLayout1);
         int last = mTextofButton.length;
         int i = 0; 
        while (i < mTextofButton.length) { if (i != last) { if (i % 2 == 0) { tr = new TableRow(this);
         mTlayout.addView(tr);
         } 
        Button btn = new Button(this);
         btn.setText(mTextofButton[i]); 
        btn.setId(i);
        btn.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) { // TODO Auto-generated method stub 
System.out.println("v.getid is:- " + v.getId());
 } }); 
 tr.addView(btn); i++; } else { //need to wr }

0 个答案:

没有答案