2011-07-29 45 views
0

我實現了一個系統,使imagebuttons等於可繪製的數量(餐廳標識),因此我過濾了以「rest」前綴開頭的圖像。它可以到這裏。然後我創建一個GridWiev並將按鈕放到它上面,而根佈局是一個RelativeLayout。但編譯後不顯示任何圖像按鈕。有我的代碼和XML。 感謝您的幫助。相對佈局問題

public class RestListActivity extends Activity{ 
    ImageButton [] buttons; 
    RelativeLayout rl; 
    int restCount=0; 
    Resources a; 
    int resIndex=0; 
    boolean resFound=false; 
    int last; 
    int resCount=0; 
    GridView gv; 
    // TODO Auto-generated constructor stub 
    @Override 
    public void onCreate (Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     rl = (RelativeLayout) findViewById(R.id.relativeLayoutRest); 
     if(rl==null) 
      Log.i("***********","RLOUT NULL AGA"); 
     else 
      Log.i("***********","RLOUT VAR AGA"); 
     setContentView(R.layout.restlist); 
     Log.i("asd","asd"); 

     gv=new GridView(this); 
     a=this.getResources(); 
     int i=0x7f020000; 
     boolean done=true; 
     if(rl==null) 
      Log.i("***********","RLOUT NULL AGA"); 
     else 
      Log.i("***********","RLOUT VAR AGA"); 
     Log.i("asd","asd"); 
     try 
     { 
      Log.i("rest buttons","______"); 
      for (i=0x7f020000 ;done&&i<0x7f020040;i++) 
      { 
       Log.i("rest buttons","----"); 
       String name=a.getResourceName(i); 
       name=name.substring(name.indexOf(':')+1); 
       name=name.substring(9); 
       if(name.startsWith("rest")) 
       { 
        if(!resFound) 
        { 
         resIndex=i; 
         resFound=true; 
        } 
        Log.i("rest buttons",name); 

        resCount++; 
       } 
      } 
     } 
     catch(Exception e) 
     { 
      last=i; 
      Log.i("LAST",i+""); 
      done=false; 
      e.printStackTrace(); 
     } 
     buttons=new ImageButton[resCount]; 
     Button orderButton = (Button)findViewById(R.id.adbutton); 
     orderButton.setText("REKLAMLAR"); 
     orderButton.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Log.i("ACTION","AD BUTTON CLICKED"); 
       try 
       { 
        Intent i = new Intent(RestListActivity.this,AVMSystemActivity.class); 
        startActivity(i); 
       } 
       catch(Exception e) 
       { 
        Log.i("cort",e.toString()); 
       } 
      } 
     }); 
     if(rl==null) 
      Log.i("------------","RLOUT NULL AGA"); 
     else 
      Log.i("------------","RLOUT VAR AGA"); 
     //************************************************************************************ 

//************************************************************************************ 
     for(int j=resIndex,f=0;j<resIndex+resCount;j++,f++) 
     { 
      try 
      { 
       Log.i("rest buttons","XXXXX"); 
       String name=a.getResourceName(j); 
       name=name.substring(name.indexOf(':')+1); 
       name=name.substring(9); 
       if(name.startsWith("rest")) 
       { 
        Log.i("BUTTON NAME",name); 
        buttons[f]=new ImageButton(this); 
        buttons[f].setImageDrawable(a.getDrawable(j)); 
        buttons[f].setOnClickListener(new View.OnClickListener() { 
         public void onClick(View view) { 
          Log.i("ACTION","BUTTON CLICKED"); 
         } 
        }); 
        //imageView.setImageDrawable(a.getDrawable(i));//------ 
        //imageView.setBackgroundDrawable(a.getDrawable(i)); 
        gv.addView(buttons[f], f); 
       } 
      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
       break; 
      } 
     } 
     if(rl==null) 
      Log.i("RLRLRL","NULL"); 
     if(gv==null) 
      Log.i("GVGVGV ","NULL"); 
     rl.addView(gv); 

    } 


} 

下面是XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayoutRest" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <Button android:layout_alignParentLeft="true" 
     android:layout_width="50px" android:id="@+id/adbutton" 
     android:layout_height="fill_parent" android:text="R\nE\nK\nL\nA\nM\nL\nA\nR" 
     android:textStyle="bold"></Button> 
    <ScrollView android:id="@+id/scrollViewRest" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
     <LinearLayout android:id="@+id/linearLayoutRest" android:paddingLeft="50px" android:layout_height="fill_parent" android:layout_width="fill_parent"></LinearLayout> 
    </ScrollView> 
</RelativeLayout> 

回答

0

寫入該行

RL =(RelativeLayout的)findViewById(R.id.relativeLayoutRest);

此行之後setContentView(R.layout.restlist);

+0

你的權利我已經這樣做了,我複製了早期版本的代碼。它就像你告訴的。休息是一樣的。但沒有顯示。只有xml中的按鈕顯示爲最左邊,但沒有網格中的圖像按鈕。 – dramaticlook

+0

任何錯誤到來和多少次「Log.i(」休息按鈕「,名稱);」印刷 –

+0

其11次。我打印,而我計算的圖像數量。現在我認識到它打印Log.i(「按鈕名稱」,名稱);只是一次之後它發現了一個錯誤。我會盡力解決這個問題:) – dramaticlook