2011-06-28 59 views
3

我已經爲我的應用程序創建了一組按鈕..現在我無法管理這些按鈕陣列的佈局..由於每當我添加圖像或chnage的寬度,它會從設備的水平屏幕熄滅..所以有任何管理這些按鈕陣列的方法,以便它們可以適應任何屏幕尺寸..我寫的代碼是..對於XML如何佈局按鈕陣列以適合任何屏幕尺寸

代碼:

public class CalenderForm extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    createCalender(); 
} 
public void createCalender() 
{ 
    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams 
    (
      RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT 
    ); 
    LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout); 
    LinearLayout rowLayout=null; 
    Button[][] buttons = new Button[6][7]; 
    int count=43; 
    for (int i = 0; i<6; i++) 
    { 
     if(count%7==1) 
     { 
      rowLayout = new LinearLayout(this); 
      layoutVertical.addView(rowLayout,p); 
      count=count-7; 
     } 
     for(int j=0;j<7;j++) 
     { 
      buttons[i][j]=new Button(this); 
      buttons[i][j].setBackgroundResource(R.drawable.icon); 
      rowLayout.addView(buttons[i][j], p); 
     } 
    } 
} 
:對Java

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

    <LinearLayout 
    android:id="@+id/liVLayout" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    </LinearLayout> 

    <LinearLayout 
    android:id="@+id/liVLayout1" 
    android:orientation="vertical" 
    android:layout_below="@+id/liVLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 

    <TextView android:text="All Contacts" 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="@android:color/black" 
     android:background="#808080"> 
    </TextView> 
    </LinearLayout> 
</RelativeLayout> 

代碼

}

快照befor插入圖片:

enter image description here

快照befor插入圖片:

enter image description here

+0

1.嘗試減少的按鈕之間的間距。 2.嘗試使用相對佈局.. – Vinay

+0

當我使用相對佈局沒有任何東西只有一個按鈕顯示.. – AndroidDev

+0

您可以添加任何數量的按鈕,你想在相對佈局..可能你有其他問題 – Vinay

回答

0

只需更換線性和按鈕的layout_weight這code.use weightsum屬性。

package com.android.manager; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class MainActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     createCalender(); 
    } 
    public void createCalender() 
    { 
     LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout); 
     LinearLayout rowLayout=null; 
     Button[][] buttons = new Button[6][7]; 
     int count=43; 
     **LayoutParams param = new LinearLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.FILL_PARENT,1);** 

     for (int i = 0; i<6; i++) 
     { 
      if(count%7==1) 
      { 
       rowLayout = new LinearLayout(this); 
       **rowLayout.setWeightSum(7);** 
       **layoutVertical.addView(rowLayout,param);** 
       count=count-7; 
      } 
      for(int j=0;j<7;j++) 
      { 
       buttons[i][j]=new Button(this); 
       buttons[i][j].setBackgroundResource(R.drawable.icon); 
       **rowLayout.addView(buttons[i][j],param);** 
      } 
     } 
    } 

} 

enter image description here

+0

感謝它的工作正常..更多的事情,它會自動調整每個屏幕的大小..或我們必須做的分離的事情 – AndroidDev

+0

嗨hitendra..one問題..當我設置每個按鈕的背景圖像比變得不活動多數民衆贊成在我無法點擊按鈕..爲什麼發生這.. – AndroidDev

1

我知道這並不直接回答你的問題,但我只是想幫助你。如果你正在構建一個日曆應用程序,創建了大量的按鍵真的是沒有要走的路:你不會有完全的控制

  1. 您將有因爲皮膚的不同ROM的問題,等等。
  2. 在佈局
  3. (因爲皮膚再次)你會遇到問題間距
  4. 你會分配大量內存(很多Button對象等),這將導致你的應用程序要慢。

我推薦的是實現您自己的自定義View。我最近自己在一個日曆應用程序上工作,並嘗試在本月使用GridView,這確實不能很好地工作(儘管看起來像它),所以我最終創建了我自己的View,這個工作非常完美。

我發現非常有用的是Android的默認,開源Calendar應用程序。您可以在這裏查找源代碼,以查看月視圖和日/周視圖(帶小時刻度等)。

+0

你..你說是至關重要的,但我的任務是使用按鈕陣列來創建自己的cuctom日曆..所以請給我一些關於這個 – AndroidDev

1

你可以嘗試TableLayout並設置列可包裝:

找到了一個不錯的教程結合實例 - >Android TableLayout Tutorial

爲了使一列可包裝(以減少它的寬度和包裹它,如果其他內容表格中的列佔用太多空間並將一些列從屏幕上移開),使用setColumnShrinkable將其標記爲可縮小。

聽起來前途

+0

+1的建議,僅僅用於鏈接。它充滿了對這些該死的幾乎不可理解的TableLayout條款的很好的解釋。 –

相關問題