2013-11-22 84 views
0

我正在嘗試創建一個帶有按鈕的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

嘗試給tableLayout設置Layout_weight爲.7和linearLayout .3 – Manishika

回答

0

看來你創建了一個新的LinearLayout和一個新的Horizo​​ntalScrollView你叫findViewById()在onCreate()方法之後。因此,您正在操作新創建的視圖,而不是在.xml文件中定義的視圖。

嘗試刪除這些行:下面code.I

hsv = new HorizontalScrollView(this); 
ll = new LinearLayout(this); 
+0

在線性佈局中使用android:alain_parentBottom =「true」,你將實現你想要的。 – Dev

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> 

希望它會有幫助。您可以詢問您是否有任何其他疑問。

0

使用android:alain_parentBottom="true"線性layout.and你會達到你想要的。

相關問題