如何创建这种按钮数组?
btnA btnB btn1 btnC
btnD btnE btnF btnJ
btnX btnN
我这样做,但按钮是垂直显示的。谁能告诉我算法?
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TableLayout;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
TableLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (TableLayout) findViewById(R.id.lay);
TableLayout.LayoutParams layoutParams = new
TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
TableLayout.LayoutParams.WRAP_CONTENT);
for(int i = 0; i < 12; i++){
Button button = new Button(this);
button.setLayoutParams(layoutParams);
button.setId(i);
button.setText("Buttun"+i);
layout.addView(button,50,20);
}
}
}
答案 0 :(得分:0)
您应该在TableRow
中创建3x TableLayout
。并将按钮放在这些行中。看看这个table layout tutorial。
实际上,您只是错误地使用TableLayout。所以你几乎可以看到任何东西。
我做了一项活动,两年前创建了一个大型的表格。
查看此处的代码
/**
* the current row of the currently selected element (starting by 0)
* @return
*/
int currentRow(int position) {
int numOnPage = position - leftTopNumber;
return numOnPage - currentColumn(position) * rowsNumber;
}
/**
* the current column of the currently selected element (starting by 0)
* @return
*/
int currentColumn(int position) {
int numOnPage = position - leftTopNumber;
return numOnPage / rowsNumber;
}
/**
* setting width and height for a ViewGroup instance
* @param viewGroup instance to be changed
* @param width if it is 0, the ViewGroup instance width remains as it was
* @param height if it is 0, the ViewGroup instance height remains as it was
*/
static void setMeasures(ViewGroup viewGroup, int width, int height) {
LayoutParams params = viewGroup.getLayoutParams();
if (width != 0) params.width = width;
if (height != 0) params.height = height;
viewGroup.setLayoutParams(params);
}
@Override
public void onResume() {
super.onResume();
}
static private LinearLayout table;
static private TextView leftTriangle, rightTriangle;
boolean fillTableByCells() {
int i = 0, j = 0;
try {
final LayoutInflater inflater =
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setContentView(R.layout.channels_list_table);
LinearLayout allForm = (LinearLayout) findViewById(R.id.AllChannelsListForm);
LinearLayout upperMargin = (LinearLayout) findViewById(R.id.UpperMargin);
LinearLayout middleStripe = (LinearLayout) findViewById(R.id.MiddleStripe);
LinearLayout bottomMargin = (LinearLayout) findViewById(R.id.BottomMargin);
RelativeLayout leftMargin = (RelativeLayout) findViewById(R.id.LeftMargin);
LinearLayout tableWrapper = (LinearLayout) findViewById(R.id.CentralFieldWrapper);
RelativeLayout rightMargin = (RelativeLayout) findViewById(R.id.RightMargin);
tvComposeNumber = (TextView) findViewById(R.id.tvComposeNumber);
leftTriangle = (TextView) findViewById(R.id.TriangleLeft);
rightTriangle = (TextView) findViewById(R.id.TriangleRight);
table = (LinearLayout) findViewById(R.id.table_channels);
allForm.measure(0, 0);
int formWidth = allForm.getWidth();
int formHeight = allForm.getHeight();
formWidth = 960;
formHeight = 540;
int heightUpper = formHeight * 66 / 570;
int heightMiddle = formHeight * 410 / 570;
int heightBottom = formHeight * 94 / 570;
int widthLeft = formWidth * 60 / 1140;
int widthMiddle = formWidth * 1020 / 1140;
int widthRight = formWidth * 60 / 1140;
setMeasures(allForm, formWidth, formHeight);
setMeasures(upperMargin, formWidth, heightUpper);
setMeasures(middleStripe, formWidth, heightMiddle);
setMeasures(bottomMargin, formWidth, heightBottom);
setMeasures(leftMargin, widthLeft, heightMiddle);
setMeasures(tableWrapper, widthMiddle, heightMiddle);
setMeasures(rightMargin, widthRight, heightMiddle);
LinearLayout.LayoutParams wrapperLP =
(LinearLayout.LayoutParams) tableWrapper.getLayoutParams();
LinearLayout.LayoutParams tableLP = (LinearLayout.LayoutParams) table.getLayoutParams();
int widthInsideTable =
widthMiddle - wrapperLP.leftMargin - wrapperLP.rightMargin
- tableWrapper.getPaddingLeft() - tableWrapper.getPaddingRight()
- tableLP.leftMargin - tableLP.rightMargin - table.getPaddingLeft()
- table.getPaddingRight();
// setMeasures(table,widthMiddle,heightMiddle);
cellsNumber = TvChannel.list.size();
columnsNumber = (cellsNumber - 1) / rowsNumber + 1;
for (i = 0; i < columnsNumber; i++) {
LinearLayout column =
(LinearLayout) inflater
.inflate(R.layout.channels_list_column, table, false);
table.addView(column, i);
for (j = 0; j < rowsNumber; j++) {
int numberChannel = i * rowsNumber + j;
if (numberChannel >= TvChannel.list.size()) break;
LinearLayout cell =
(LinearLayout) inflater.inflate(R.layout.channels_list_cell, column,
false);
column.addView(cell, j);
TextView numberChannelView = (TextView) cell.getChildAt(0);
TextView nameChannelView = (TextView) cell.getChildAt(1);
nameChannelView.setText(TvChannel.list.get(numberChannel).name);
String numberString = String.format("%d", i * rowsNumber + j);
numberChannelView.setTextSize(new float[] { 22f, 20f, 17f }[numberString
.length() - 1]);
numberChannelView.setText(numberString);
cell.setVisibility(View.VISIBLE);
}
// column.setLayoutParams(new
// LayoutParams(tableWidth/3,LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams columnLP =
(LinearLayout.LayoutParams) column.getLayoutParams();
int colWidth =
widthInsideTable / maxSeenColumns - columnLP.leftMargin
- columnLP.rightMargin;
setMeasures(column, colWidth, LayoutParams.MATCH_PARENT);
for (int iCell = 0; iCell < column.getChildCount(); iCell++) {
LinearLayout cell = (LinearLayout) column.getChildAt(iCell);
setMeasures(cell, LayoutParams.MATCH_PARENT,
(heightMiddle - column.getPaddingBottom() * 2) / rowsNumber);
}
}
return true;
}
catch (Throwable e) {
ErrorMessage.outputMessageByName("channels_list_activity_create", this, e.toString()
+ "; column=" + i + "; row=" + j);
finish();
return false;
}
}
答案 1 :(得分:0)
您可以将垂直LinearLayout与包含按钮的水平子项一起使用。
android:layout_width="0dp"
android:layout_weight="1"
意味着它将占用尽可能多的水平空间,所以这样所有按钮均匀延伸。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button6"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button7"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button8"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button9"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button10"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Button" />
<View
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
答案 2 :(得分:0)
动态地想要添加按钮,
粗略的想法是,
使用main linear layout
vertical orientation.
和sublayout1
horizontal orientation
布局gravity center
。
和sublayout2,水平方向,布局重心。
和sublayout3,水平方向,布局重心。
和sublayout4,水平方向,布局重心。
在
上添加按钮然后
sublayout1.add(btn1)
sublayout1.add(btn2)
sublayout1.add(btn3)
sublayout1.add(btn4)
mainliniarlayout.add(sublayout)
类似地添加其他按钮和布局。
动态添加按钮,请阅读以下帖子,
答案 3 :(得分:0)