2012-02-09 100 views
6

這裏你是我的問題:我想要做的佈局看起來像這樣的網格,安卓的Android tablelayout和按鈕調整

enter image description here

爲了做到這一點,我已經創造了這個佈局:

 <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3"> 

     <Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 



     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" > 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button"/> 

     <Button 
      android:id="@+id/button6" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" /> 

    </TableRow> 

    <TableRow 
     android:id="@+id/tableRow3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.3" > 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" 
      /> 

     <Button 
      android:id="@+id/button7" 
      android:layout_width="0dip" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:text="Button" 
      /> 

    </TableRow> 

    </TableLayout> 

    </RelativeLayout> 

所以基本上它是由6個按鈕在表格佈局中設置正確的重量屬性(或者我希望如此!)。結果看起來不錯,直到我嘗試設置每個按鈕的背景屬性。在這種情況下,按鈕將放入圖像的高度(寬度可以)。舉例來說,如果我設置背景圖片,其尺寸爲96PX X 96PX的結果是這樣的:

enter image description here

是否有辦法防止這種按鈕「拉伸」,並正確中心的形象呢? 我試着改變每個表格行的高度屬性,改變按鈕的最大高度屬性,也用圖像按鈕更改按鈕類型(並設置與想要的圖標src屬性),但我沒有完成通緝結果。
我也紅了有關支持多個屏幕和每個基本佈局的谷歌文檔,但我沒有找到任何解決方案。 在此先感謝任何願意幫助我的人!

安德烈

+0

嘗試在按鈕上使用android:scaleType =「fitXY」。也嘗試和使用http://developer.android.com/resources/tutorials/views/hello-gridview.html這項任務 – 2012-02-09 23:15:56

+0

我試過屬性android:scaleType =「fitXY」,但它沒有奏效。如果我找不到解決方案,我會嘗試切換到網格佈局。謝謝 – jiraya85 2012-02-09 23:29:24

回答

3

設置表格行作爲0dip的佈局高度,並設置爲1

<TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

這應該可以解決您的問題


更改重量

<Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 

to

<Button 
      android:id="@+id/clientimain" 
      android:layout_width="0dip" 
      android:layout_weight="1" android:layout_height="fill_parent"/> 

這可能是工作,值得一試,在eclipse對我來說它呈現的權利,你怎麼過的第一次,也許你可以試試清潔您的項目(Project>清潔工程),因爲它應該是正確的,因爲它是


你的代碼應該是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/clientimain" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" /> 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button2" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button6" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 

     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <Button 
       android:id="@+id/button3" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 

      <Button 
       android:id="@+id/button7" 
       android:layout_width="0dip" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:text="Button" /> 
     </TableRow> 
    </TableLayout> 

    </RelativeLayout> 

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center" 
    android:orientation="vertical" > 


    <TableLayout 
     android:id="@+id/tableLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 


     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton1" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton2" 
       android:layout_width="0dp" 
       android:layout_height="fill_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 


     <TableRow 
      android:id="@+id/tableRow2" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton3" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton4" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 


     <TableRow 
      android:id="@+id/tableRow3" 
      android:layout_width="wrap_content" 
      android:layout_height="0dp" 
      android:layout_weight="1" > 

      <ImageButton 
       android:id="@+id/imageButton5" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 

      <ImageButton 
       android:id="@+id/imageButton6" 
       android:layout_width="0dp" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:src="@drawable/ic_launcher" /> 
     </TableRow> 

    </TableLayout> 

    </RelativeLayout> 

仍然沒有改變任何東西,圖像的大小有多大?

+0

我已經試過這樣做,但沒有result.Now表中的每一行都得到了屬性(高度0和重量1),但我仍然有這種舒展。謝謝 – jiraya85 2012-02-10 00:30:44

+0

首先感謝您的支持。我試圖清理這個項目,並且用你的代碼替換我的代碼,但是當我選擇一個背景圖片時,我仍然有同樣的問題。如果我沒有應用任何圖像的按鈕的背景一切都很好,網格看起來像我想要的! – jiraya85 2012-02-10 01:00:54

+0

嗯好吧,生病嘗試與圖像然後,只是等待幾個,生病回來 – FabianCook 2012-02-10 01:01:52

2
+0

非常感謝!這工作完美。我非常感謝你的幫助!非常感謝!!! – jiraya85 2012-02-10 01:50:37

+0

你還需要其他人嗎? – FabianCook 2012-02-10 01:51:13

+0

我真的很感謝你的幫助,但我不想打擾你!你做得夠多了!我會嘗試自己做9個補丁。非常感謝SmartLemon! – jiraya85 2012-02-10 01:56:50

0

當我申請的背景圖像

android:background="@drawable/ic_launcher" 

它工作得很好。

我以爲這是因爲圖像的大小,所以我應用其他圖像的大小160X160和問題回來了。 比我應用相同的160X160圖像到所有的按鈕,他們完美對齊。 這裏是我的代碼(圖像Gender.png是160×160像素)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/RelativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:gravity="center" 
android:orientation="vertical" > 

<TableLayout 
android:id="@+id/tableLayout1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TableRow 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:layout_weight="0.3"> 

    <Button 
     android:id="@+id/clientimain" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:background="@drawable/Gender" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" > 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

    <Button 
     android:id="@+id/button6" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 

<TableRow 
    android:id="@+id/tableRow3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" 
    > 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" /> 

    <Button 
     android:id="@+id/button7" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender"/> 

</TableRow> 


    <TableRow 
    android:id="@+id/tableRow4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_weight="0.3" > 

    <Button 
     android:id="@+id/button77" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" 
     /> 

    <Button 
     android:id="@+id/button75" 
     android:layout_width="0dip" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:text="Button" 
     android:background="@drawable/Gender" 
     /> 

</TableRow> 


</TableLayout> 

1

我知道這不正是你想要的,但我建議使用替代的TableRow佈局GridView控件。

我認爲GridView應該更好地工作,因爲它實現了視圖回收和從AbsListView繼承的東西。 GridView更難以部署,因爲您必須使用適配器,但如果您有許多沉重的視圖需要加載(例如圖像),它將會有效地工作。 Reference.

<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/gridView1" 
    android:numColumns="auto_fit" 
    android:stretchMode="columnWidth" 
    android:columnWidth="100dp" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

</GridView>