我可以控制高度但不知何故它对按钮宽度没有任何影响。
// Table Row for Previous button
TableRow tableRow1 = new TableRow(this);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow1.setPadding(5, 5, 5, 5);
// Button - Previous
Button btnPrev = new Button(this);
btnPrev.setText("< Previous");
btnPrev.setGravity(Gravity.CENTER);
btnPrev.setOnClickListener(prevListener);
tableRow1.addView(btnPrev);
android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
params.height = 20;
params.width = 60;
btnPrev.setLayoutParams(params);
// TextView for Previous button
TextView textView11 = new TextView(this);
textView11.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
textView11.setGravity(Gravity.CENTER_VERTICAL);
textView11.setGravity(Gravity.RIGHT);
textView11.setPadding(0, 0, 20, 0);
textView11.setText("New Entry Form");
tableRow1.addView(textView11);
android.view.ViewGroup.LayoutParams params1 = textView11
.getLayoutParams();
params1.height = (int) dipHeight;
textView11.setLayoutParams(params1);
tableLayout.addView(tableRow1);
我试着在这里和那里改变一些东西但不看。
你们这些人可以花时间帮助我吗?
答案 0 :(得分:1)
// replace this code
tableRow1.addView(btnPrev);
android.view.ViewGroup.LayoutParams params = btnPrev.getLayoutParams();
params.height = 20;
params.width = 60;
btnPrev.setLayoutParams(params);
to
tableRow1.addView(btnPrev,new android.view.ViewGroup.LayoutParams(20,60));
答案 1 :(得分:0)
设置按钮宽度&amp;高度使用低于代码
btnPrev.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnPrev.setwidth(60);
btnPrev.setheight(20);
答案 2 :(得分:0)
您可以通过两种方式设置按钮宽度:
通过java:
Button.setWidth(int width);
通过XML属性:
<Button android:layout_height="wrap_content"
android:id="@+id/ButtonPrev"
android:layout_below="@id/output"
android:text="7"
android:minWidth="100dp">
</Button>
属性:android:minWidth
将设置按钮的宽度。
答案 3 :(得分:0)
View.requestLayout()
也许你想要什么?