2013-07-06 21 views
0

我膨脹一個xml使用LayoutInflater,因爲我想這個視圖多次(動態)。Overiding onFinishInflate()方法

我ònCreate`方法如下:

protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.home); 
        v[i]= new View[4]; 
      LinearLayout linear= (LinearLayout) findViewById(R.id.linearLayout); 
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      for(int i = 1; i <4; i++) { 

      v[i]= inflater.inflate(R.layout.row, linear);       //v[i] returns view i.e. Linear Layout from my row.xml 
      } 
     } 

我對row.xml代碼如下 -

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="#ffaf04" 
    android:orientation="horizontal" 
    android:padding="10dip" > 

    <LinearLayout 
     android:id="@+id/rel1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="5dp" 
     android:background="#ffffff" 
     android:orientation="vertical" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/imageView1inInflatedRow" 
      android:layout_width="130dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/image_holder" > 
     </ImageView> 

     <Button 
      android:id="@+id/button1inInflatedRow" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_marginTop="5dp" 
      android:background="@layout/button_selector" 
      android:padding="5dip" 
      android:textColor="#FFFFFF" 
      android:textSize="12sp" > 
     </Button> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="5dp" 
     android:background="#ffffff" 
     android:orientation="vertical" 
     android:padding="5dp" > 

     <ImageView 
      android:id="@+id/imageView2inInflatedRow" 
      android:layout_width="130dp" 
      android:layout_height="wrap_content" 
      android:adjustViewBounds="true" 
      android:src="@drawable/image_holder" > 
     </ImageView> 

     <Button 
      android:id="@+id/button2inInflatedRow" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:layout_marginTop="5dp" 
      android:background="@layout/button_selector" 
      android:padding="5dip" 
      android:textColor="#FFFFFF" 
      android:textSize="12sp" > 
     </Button> 
    </LinearLayout> 

</LinearLayout> 

現在行包含按鈕,我想設置一個onclick監聽器按鈕即button1inInflatedRowbutton2inInflatedRow這些按鈕執行相同的功能,但我需要知道哪個按鈕調用了該功能。那麼,如何爲這些按鈕撥打findVieById()?或者有沒有其他的方式來調用按鈕onclick ..?

+0

*螞蟻爲按鈕設置Onclicklistener。* - 膨脹佈局後,在新膨脹的佈局中找到這些按鈕並分配偵聽器。 *我知道監聽器必須在View.OnFinishInflate()方法中。* - 也許如果你製作自己的自定義視圖。 – Luksprog

+0

當你在你的問題中聲明OnFinishInflate是一個視圖方法而不是一個活動,所以你不能在你的活動中覆蓋它。 – Christos

+0

@Christos所以在哪裏覆蓋方法? –

回答

1

的最佳方式(按我的知識),膨脹行如下 -

View v[i] = inflater.inflate(R.layout.row, null); 
    linear.addView(v[i]); 

這款V在row.xml中返回充氣的視圖。因此,我們可以我用

View v[i] = inflater.inflate(R.layout.row, linear); 

這款V訪問按鈕

photoButton[i]=(Button) v[i].findViewById(R.id.button1inInflatedRow); 

我哪裏錯了--- [i]是父佈局的,因此我通過

不可能訪問按鈕

(Button) v[i].findViewById(R.id.button1inInflatedRow)

3

您不需要onFinishInflate()。撥打inflate()會返回新創建的視圖。只需在按鈕上逐個使用findViewById(),並在每個按鈕上設置OnClickListener。

+0

setContentView(R.layout.home); LinearLayout linear =(LinearLayout)findViewById(R.id.linearLayout); LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); (int i = 1; i <4; i ++){ inflater.inflate(R.layout.row,linear); } –

0

據我瞭解,您的佈局R.layout.row包含Buttons,並且您希望將OnClickListeners添加到這些Buttons。你不需要重寫任何方法來做到這一點。 LayoutInflater的方法inflate()返回您剛膨脹的View的實例。然後,您可以使用findViewById()獲取佈局中的Buttons。嘗試是這樣的:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
View v = inflater.inflate(R.layout.row, linear); 
Button buttonOne = (Button) v.findViewById(R.id.buttonOne); 
button.setOnClickListener(new View.OnClickListener()... 
+0

v.findViewById(R.id.buttonOne);返回LinearLayout ..我怎麼能找到LinearLayout >> LinearLayout >> Button中的按鈕。 –

+1

查看v = inflater.inflate(R.layout.row,null); linear.addView(v); 這對我來說很有效。這種方式更容易膨脹視圖。 –

0

要準確地回答你的問題是:

那麼,如何以及在哪裏重寫此方法?

答案在View或ViewGroup子類中。

但很明顯,您正在問如何在LinearLayout中「重複」的「行」佈局中爲按鈕應用偵聽器。爲了實現這一點,不需要重寫此方法,但有些事情需要清除纔能有更好的答案。例如,「行」佈局中的按鈕有多少?你想每行中的每個按鈕做同樣的事情(調用同一個函數),或者按鈕的功能必須不同,比如說它們是基於哪一行?

我會給你一個關於如何做到這一點的總體想法,但它完全基於我的想法,如果你需要更好的答案,你應該編輯你的問題,並提供更多的細節,如你的XML文件,你到底在想什麼去做。

現在的代碼。

在你的「行」 XML佈局提供下列屬性的按鈕的元素:

android:onClick="ButtonClickFunction" 

然後在你的活動聲明這樣的功能:

public void ButtonClickFunction(View v) { 
    <Your code here> 
} 

現在,只要點擊此按鈕(在任何一行)ButtonClickFunction將被調用,按鈕作爲參數。但是,如果您想要基於行號的不同操作,這可能不是最好的方法。

當然,你可以改變「ButtonClickFunction」名稱與任何你想要的。你可以閱讀更多關於我的建議herehere.

希望這有助於...

+0

謝謝!這真的幫助..但需要更多的幫助..請檢查編輯的問題.. –

+0

您可以創建兩個不同的onClick屬性,每個按鈕一個,有兩個不同的功能,例如android:onClick =「button1inInflatedRowClick」和android:onClick = 「button2inInflatedRowClick」在行xml中,然後在您的活動中創建兩個公共功能,如public void button1inInflatedRowClick(View v)和public void button2inInflatedRowClick(View v)。這樣,每個按鈕都會調用每個函數,而且根本不需要getViewById。 – Christos