Android 相对类yout表布局
Hi a拥有RelativeLayout并希望显示6个按钮,如“TableLayout”,i calc大小按钮并尝试在活动上显示。只显示最后一个按钮 调用方法时,我尝试使用6个按钮(buttonsperscreen=6)执行活动 你能帮我吗 代码是:Android 相对类yout表布局,android,android-layout,Android,Android Layout,Hi a拥有RelativeLayout并希望显示6个按钮,如“TableLayout”,i calc大小按钮并尝试在活动上显示。只显示最后一个按钮 调用方法时,我尝试使用6个按钮(buttonsperscreen=6)执行活动 你能帮我吗 代码是: private void doLayoutWithButtons(int buttonsperscreen) { // if total butons is % 2 == 0 if (buttonsperscreen % 2 ==
private void doLayoutWithButtons(int buttonsperscreen) {
// if total butons is % 2 == 0
if (buttonsperscreen % 2 == 0) {
// get display dimensions
DisplayMetrics d = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(d);
int w = d.widthPixels;
int h = d.heightPixels;
// calc size of buttons
int widthButton = w / 2; // only two columns of buttons.
int heightButton = h / ((buttonsperscreen) / 2); // sample 100px /
// (6/2) = 3
int posx = 0;
int posy = 0;
for (int i = 1; i <= buttonsperscreen; i++) {
Button newButton = new Button(this);
newButton.setId(100 + i + 1); // ID of zero will not work
newButton.setText("Botão: " + i);
// atribuindo o tamanho do botão.
newButton.setHeight(heightButton);
newButton.setWidth(widthButton);
newButton.setId(1000 + i);
// positioning buttons...
RelativeLayout layout1 = new RelativeLayout(this);
// set layout with size of button
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
widthButton, heightButton);
params.leftMargin = posx;
params.topMargin = posy;
newButton.setLayoutParams(params);
layout1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(newButton);
setContentView(layout1);
// to calc positions x,y for each button for next iteration
if (posx + widthButton < w) {
posx = posx + widthButton;
} else {
posx = 0;
posy = posy + heightButton;
}
}
}
}
private void dolayouth按钮(int按钮屏幕){
//如果总Buton为%2==0
如果(按钮屏幕%2==0){
//获取显示尺寸
DisplayMetrics d=新的DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(d);
int w=d.1像素;
int h=d.高度像素;
//按钮的计算大小
int widthButton=w/2;//只有两列按钮。
int heightButton=h/((按钮屏幕)/2);//样本100px/
// (6/2) = 3
int posx=0;
int-posy=0;
对于(int i=1;i如果您试图在屏幕上均匀地显示按钮,则不需要在代码中执行此操作。您可以在布局中指定类似的内容
类似这样的东西将使6个按钮的宽度相等,水平显示
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="@+id/button1"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="@+id/button2"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="@+id/button3"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="@+id/button4"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:layout_height="wrap_content" android:id="@+id/button5"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
<Button android:layout_height="wrap_content" android:id="@+id/button6"
android:text="Button" android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
只要您将布局宽度保持为0dp,并将权重设置为1,Android将自动将宽度设置为相等,无论您有多少个按钮。您不能使用XML绘制所需布局吗?如果您想使用类似线性布局的相对布局,我必须问……您为什么不使用线性布局?不,亲爱的。。。我需要6个按钮,比如6个“表格单元格”,明白吗?我不确定这到底意味着什么。我已经调整了它,使它每行有2个按钮,但也许你可以澄清一下“表格单元格”的含义。你有布局的例子吗?