2013-08-12 23 views
2

我正在使用RelativeTexts,Buttons等一些項目來設置自定義ActionBar視圖的RelativeLayout。問題是我無法獲得那個自定義視圖中的項目的引用。無法獲取自定義ActionBar中的項目的引用

這裏是動作條

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@color/actionbar_background" 
android:gravity="fill_horizontal|center_horizontal" > 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:gravity="center_horizontal" 
     android:paddingTop="10dp" > 

     <ImageButton 
      android:id="@+id/ab_home" 
      android:layout_width="47dp" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:paddingLeft="6dp" 
      android:paddingRight="6dp" 
      android:src="@drawable/home" /> 
    </LinearLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="match_parent" 
     android:alpha="0.2" 
     android:background="@color/left_menu_background" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="6" 
     android:orientation="horizontal" > 

     <LinearLayout 
      android:id="@+id/ab_action_search" 
      android:layout_width="match_parent" 
      android:padding="7dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:visibility="gone" > 

      <EditText 
       android:id="@+id/et_search" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="@drawable/rounded_edittext" 
       android:drawablePadding="8dp" 
       android:drawableRight="@drawable/action_search_light" 
       android:hint="Search" 
       android:imeOptions="actionSearch" 
       android:paddingLeft="5dp" 
       android:singleLine="true" 
       android:cursorVisible="false" 
       android:textColor="#000000" 
       android:textSize="15dp" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/ab_action_buttons" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:paddingTop="10dp" 
      android:visibility="visible" > 

      <Button 
       android:id="@+id/btn_customAB_font" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@null" 
       android:text="@string/icon_font" 
       android:textColor="#FFFFFF" 
       android:textSize="10dp" /> 

      <Button 
       android:id="@+id/btn_customAB_tasks" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@null" 
       android:text="" 
       android:textColor="#FFFFFF" 
       android:textSize="25dp" /> 

      <Button 
       android:id="@+id/btn_customAB_search" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:background="@null" 
       android:clickable="true" 
       android:text="@string/icon_search" 
       android:textColor="#FFFFFF" 
       android:textSize="25dp" /> 
     </LinearLayout> 
    </LinearLayout> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="match_parent" 
     android:alpha="0.2" 
     android:background="@color/left_menu_background" /> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:paddingTop="10dp" > 

     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:src="@drawable/ic_action_user" /> 
    </LinearLayout> 
</LinearLayout> 

</RelativeLayout> 

自定義視圖這裏是我膨脹和動作條的setCustomView

ActionBar ab = getSupportActionBar(); 

    ab.setDisplayShowCustomEnabled(true); 
    ab.setDisplayHomeAsUpEnabled(false); 
    ab.setDisplayShowHomeEnabled(false); 
    ab.setDisplayUseLogoEnabled(false); 
    ab.setCustomView(R.layout.custom_actionbar); 

inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
customActionBarView = inflater.inflate(R.layout.custom_actionbar,null); 
ImageButton ib = (ImageButton) customActionBarView.findViewById(R.id.ab_home); 

ib.setOnClickListener(new OnClickListener() { 
    @Override 
     public void onClick(View v) { 
     Log.d(TAG, "Home Clicked"); 
     } 
    }); 

該日誌語句從不運行。我認爲我以錯誤的方式使用findViewById()來引用按鈕。

任何幫助將不勝感激。

+0

只是使用的ImageButton IB =(的ImageButton)findViewById(R.id.ab_home);看到這個答案 - > http://stackoverflow.com/a/16079425/1168654 –

+0

OMG,這只是一個小事情,我被困在它上面4個小時。這個修改做到了,謝謝 你可以添加它作爲答案我會接受它 順便說一句你能說出爲什麼用CustomActionBar引用它並沒有做這項工作嗎? –

回答

3

正如@Dhawal Sodha所述。除了使用

ImageButtonib=(ImageButton)CustomActionBarView.findViewById(R.id.ab_home);

使用

ImageButtonib=(ImageButton)findViewById(R.id.ab_home);

相關問題