2013-08-19 50 views
2

我爲我的ActionBar設置了自定義視圖,我想啓用homeAsUp。一切正常,Up指示燈可點擊。現在我想讓自定義視圖可點擊和觸摸(就像普通的應用程序圖標一樣)。所以我想將狀態按下(懸停)效果也應用到自定義視圖。理想情況下,向上指標和自定義視圖顯示爲一個區域。因此,當我點擊/觸摸向上指示器時,自定義視圖也應該改變其選擇器,反之亦然。有沒有簡單的方法來做到這一點?ActionBar:HomeAsUp與自定義視圖

順便說一句:我不能使動作條自定義視圖的應用指標的部分,因爲我用ActionBarDrawerToggle ...

回答

0

您可以使用程序兼容性支持庫支持「工具欄」做到這一點。

0

首先,您必須創建自定義操作欄的佈局,以便根據您的要求定義功能。這裏是xml文件...

佈局自定義動作條:

<ImageView 
     android:id="@+id/custom_actionbar_back_iv" 
     android:layout_width="@dimen/small_margin_ar" 
     android:layout_height="@dimen/very_small_margin_ar" 
     android:layout_alignParentLeft="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/up_navigation"/> 

    <TextView 
     android:id="@+id/custom_actionbar_titleText_tv" 
     style="@style/wrapscreen" 
     android:layout_centerVertical="true" 
     android:layout_toRightOf="@+id/custom_actionbar_back_iv" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:textColor="@color/white" 
     android:textSize="@dimen/above_medium_text_size" /> 

    <ImageButton 
     android:id="@+id/custom_actionbar_goToArchiveActivity_ib" 
     style="@style/wrapscreen" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     android:layout_marginRight="@dimen/medium_margin" 
     android:background="@null" 
     android:src="@drawable/ic_action_overflow" /> 

</RelativeLayout> 

這裏去執行..

定義setupactionBar()在onCreate方法()方法

private void setUpActionBar() { 
      ActionBar actionBar = getActionBar(); 
      actionBar.setDisplayShowHomeEnabled(false); 
      actionBar.setDisplayShowTitleEnabled(false); 

      LayoutInflater layoutInflater = LayoutInflater.from(this); 
      View customActionBarView = layoutInflater.inflate(R.layout.custom_actionbar, null); 

      // this view is used to show tile 
      TextView ActivityTitleTV = (TextView) customActionBarView.findViewById(R.id.custom_actionbar_titleText_tv); 
      ActivityTitleTV.setText(R.string.title_text_archive); 

      //this view can be used for back navigation 
      ImageView backToRecorderIV = (ImageView) customActionBarView.findViewById(R.id.custom_actionbar_back_iv); 
      backToRecorderIV.setVisibility(View.VISIBLE); 
      backToRecorderIV.setOnClickListener(this); 

      //Another view which has up navigation 
      ImageButton goToArchiveActivityIB = (ImageButton) customActionBarView.findViewById(R.id.custom_actionbar_goToArchiveActivity_ib); 
      goToArchiveActivityIB.setVisibility(View.GONE); 

      actionBar.setCustomView(customActionBarView); 
      actionBar.setDisplayShowCustomEnabled(true); 
     } 

     //listener for back navigation 
     @Override 
     public void onClick(View view) { 
      switch (view.getId()) { 
       case R.id.custom_actionbar_back_iv: 
        Intent recorder = new Intent(ArchiveActivity.this, RecorderActivity.class); 
        startActivity(recorder); 
        finish(); 
        break; 
      } 
     } 

希望這會有所幫助...