我正在尝试创建一个带有按钮的5x5表,占用屏幕空间的70%。在这下面我想用滚动条水平放置按钮(在屏幕的底部意味着)。 java和xml文件如下所示。 在输出屏幕上,我只能看到创建的5x5表格,底部空格留空。我是Android和布局的新手,所以请帮助我。谢谢!
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TableLayout
android:id="@+id/mainPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TableLayout>
<HorizontalScrollView
android:id="@+id/hsvMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/scroll_ll"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:isScrollContainer="true"
android:scrollbars="horizontal"
android:orientation="horizontal">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
Java文件:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.match_main_skl);
table = new TableLayout (this);
setControls();
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
mainTable();
}
private void setControls()
{
table = (TableLayout) findViewById(R.id.mainPage);
hsv = (HorizontalScrollView) findViewById(R.id.hsvMain);
ll = (LinearLayout) findViewById(R.id.scroll_ll);
}
private void mainTable()
{
//find screen size of the device (supports all versions)
Point windowSize = MAUIUtil.GetDefaultWindowSize(this);
int cellSize;
if (windowSize.x >= 0.70 * windowSize.y)
cellSize = windowSize.x / numColumns;
else cellSize = (int) ((0.70*windowSize.y)/numColumns);
**//5x5 TABLE**
TableRow.LayoutParams buttonRow = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1.0f);
buttonRow.height = cellSize * numRows;
//buttonRow.height = windowSize.x;
table.setLayoutParams(buttonRow);
table.setStretchAllColumns(true);
for(int rw = 0; rw < numRows; rw++)
{
TableRow row = new TableRow(this);
row.setPadding(0, 0, 0, 0); //setPadding(left,top,right,bottom)
row.setId(ROW_ID_OFFSET + rw);//giving each row an id
TableLayout.LayoutParams tbLp = new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1.0f);
tbLp.height = cellSize;
table.setLayoutParams(tbLp);
for(int col = 0; col < numColumns; col++)
{
int btnID = COLUMN_ID_OFFSET + (numColumns * (rw - 1)) + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.CYAN);
btn.setText(Integer.toString(btnID));
row.addView(btn);
}
table.addView(row);
}
**//LAST SCROLLABLE HORIZONTAL ROW**
hsv.addView(ll);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
llp.height = windowSize.y - cellSize;
hsv.setLayoutParams(llp);
LinearLayout sll = new LinearLayout(this);
for(int col = 0; col < scrNumColumns; col++)
{
int btnID = SCROLL_COLUMN_ID_OFFSET + col;
//Adding buttons
Button btn = new Button(this);
btn.setId(btnID);
btn.setPadding(0, 0, 0, 0);
btn.setTypeface(Typeface.DEFAULT_BOLD);
btn.setBackgroundColor(Color.RED);
btn.setText(Integer.toString(btnID));
sll.addView(btn);
sll.setGravity(Gravity.BOTTOM);
}
ll.addView(sll);
}
答案 0 :(得分:0)
在onCreate()方法中调用findViewById()之后,似乎创建了一个新的LinearLayout和一个新的HorizontalScrollView。因此,您正在操作新创建的视图,而不是.xml文件中定义的视图。
尝试删除这些行:
hsv = new HorizontalScrollView(this);
ll = new LinearLayout(this);
答案 1 :(得分:0)
使用下面的代码。我已经按照你的描述实现了它。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
<TableRow > </TableRow>
</TableLayout>
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button4"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button5"/>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
希望它会有所帮助。您可以询问是否有任何进一步的询问。
答案 2 :(得分:0)
在线性布局中使用android:alain_parentBottom="true"
。您将实现您想要的效果。