2012-06-26 21 views
0

如何将TableLayout放置在屏幕中心5上的按钮5的网格?TableLayout放置

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/Table5" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="30dp" 
      android:weightSum="5"/> 

回答

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_centerInParent="true" 
     android:layout_margin="10dp" 
     android:background="#ffffff" 
     android:gravity="center" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b11" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b12" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b13" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b14" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b15" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b21" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b22" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b23" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b24" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b25" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b31" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:text="b32" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b33" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b34" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b35" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b41" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b42" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b43" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b44" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b45" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal" > 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b51" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b52" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b53" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b54" /> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="b55" /> 
     </TableRow> 
    </TableLayout> 

</RelativeLayout> 

使用上面的代码在Android的布局XML文件来创建使用TableLayout

+0

Yeee))谢谢)) – Ramin

+0

很高兴我帮你。你会介意它作为答案吗? – sunil

0

尝试增加android:layout_gravity="center"属性来TableLayout

(如果不工作,包裹TableLayoutLinearLayout或类似的东西。)

0
<GridView 
     android:id="@+id/myGrid" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:columnWidth="@dimen/grid_width" 
     android:gravity="center" 
     android:numColumns="auto_fit" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:stretchMode="spacingWidth" 
     android:verticalSpacing="8dp" /> 

进入grid_width,你可以设置一个尺寸(对于hdpi,480/5)。 我不知道这是否适合您的情况。但尝试

+0

Android平台5x5的按键矩阵:weightSum = “5” 不meen:5行5列。例如,当您可以将每个项目的空间设置为linearLayout时,就像使用“百分比”一样。 – marceloamx