2013-08-30 52 views
1

通過使用動作欄支持庫,我試圖將自定義視圖與選項卡導航一起放到動作欄中。但這並不像預期的那樣。動作欄支持庫 - 自定義視圖和製表符

以下是代碼。

public class TODOListHomeActivity extends BaseActivity { 

private RelativeLayout topbarLayout; 
private TextView titleTextView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    _activity = this; 
    // Notice that setContentView() is not used, because we use the root 
    // android.R.id.content as the container for each fragment 

    // setup action bar for tabs 
    actionBar = getSupportActionBar(); 
    // disable the default title and icon display 
    actionBar.setDisplayShowHomeEnabled(false); 
    actionBar.setDisplayShowTitleEnabled(false); 
    actionBar.setLogo(null); 
    View homeIcon = findViewById(android.R.id.home); 
    ((View) homeIcon.getParent()).setVisibility(View.GONE); 

    // TODO: The below code may change as per new design 
    topbarLayout = (RelativeLayout)LayoutInflater.from(_activity).inflate(getResources().getLayout(R.layout.header_layout), null); 
    actionBar.setCustomView(topbarLayout); 
    titleTextView = (TextView)topbarLayout.findViewById(R.id.tv_messagecenter); 
    titleTextView.setText(getResources().getText(R.string.todolist)); 
    // new design-end 

    actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    Tab tab = actionBar.newTab() 
      .setText(getResources().getString(R.string.tasks)) 
      .setTabListener(new TabListener<TODOListFragment>(
        this, getResources().getString(R.string.tasks), TODOListFragment.class)); 
    actionBar.addTab(tab); 

    tab = actionBar.newTab() 
      .setText(getResources().getString(R.string.due_today)) 
      .setTabListener(new TabListener<TODOListFragment>(
        this, getResources().getString(R.string.due_today), TODOListFragment.class)); 
    actionBar.addTab(tab); 

    tab = actionBar.newTab() 
      .setText(getResources().getString(R.string.completed)) 
      .setTabListener(new TabListener<TODOListFragment>(
        this, getResources().getString(R.string.completed), TODOListFragment.class)); 
    actionBar.addTab(tab); 
} 

class TabListener<T extends Fragment> implements ActionBar.TabListener { 
    private Fragment mFragment; 
    private final Activity mActivity; 
    private final String mTag; 
    private final Class<T> mClass; 

    /** Constructor used each time a new tab is created. 
    * @param activity The host Activity, used to instantiate the fragment 
    * @param tag The identifier tag for the fragment 
    * @param clz The fragment's Class, used to instantiate the fragment 
    */ 
    public TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 

    /* The following are each of the ActionBar.TabListener callbacks */ 

    public void onTabSelected(Tab tab, FragmentTransaction ft) { 
     // Check if the fragment is already initialized 
     if (mFragment == null) { 
      // If not, instantiate and add it to the activity 
      mFragment = Fragment.instantiate(mActivity, mClass.getName()); 
      ft.add(android.R.id.content, mFragment, mTag); 
     } else { 
      // If it exists, simply attach it in order to show it 
      ft.attach(mFragment); 
     } 
    } 

    public void onTabUnselected(Tab tab, FragmentTransaction ft) { 
     if (mFragment != null) { 
      // Detach the fragment, because another one is being attached 
      ft.detach(mFragment); 
     } 
    } 

    public void onTabReselected(Tab tab, FragmentTransaction ft) { 
     // User selected the already selected tab. Usually do nothing. 
    } 
} 

和header_layout.xml是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res/com.vl.infotrax" 
style="?attr/actionButtonStyle" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="fill_horizontal" 
android:background="@drawable/topstrip" > 

<ImageView 
    android:id="@+id/iv_crossicon" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_centerVertical="true" 
    android:layout_marginLeft="@dimen/margin_small" 
    android:src="@drawable/crossicon" /> 

<com.vl.infotrax.components.CustomTextView 
    android:id="@+id/tv_messagecenter" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:layout_marginLeft="@dimen/margin_small" 
    android:layout_toLeftOf="@+id/btn_brodcast_menu" 
    android:layout_toRightOf="@+id/iv_crossicon" 
    android:ellipsize="end" 
    android:gravity="center" 
    android:lines="1" 
    android:maxLines="1" 
    android:singleLine="true" 
    android:text="MESSAGE CENTER" 
    android:textColor="@android:color/white" 
    android:textSize="@dimen/textsize_titlebar" 
    custom:customFont="fonts/HELVETIC.TTF" /> 

<ImageView 
    android:id="@+id/btn_brodcast_menu" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true" 
    android:layout_marginRight="@dimen/margin_small" 
    android:background="@drawable/broadcasticon" /> 

</RelativeLayout> 

輸出類似於如下。

Layout Link

我需要的標籤欄應該來定製視圖下方。我怎樣才能做到這一點?

+0

以及自定義視圖是一個動作吧? – Piyush

+0

這不是操作欄。但我想用它作爲操作欄的自定義視圖。 – Kameswari

+0

如果刪除了actionBar.setDisplayShowHomeEnabled(false),它是否工作? 請參閱https://code.google.com/p/android/issues/detail?id=36191,評論2(您已使用)和3. –

回答

0

在你的情況下,一個ActionBar嵌入每個標籤,而不是所有標籤一個,但你需要ActionBarViewPager,相反的方式。看看這個教程 http://wptrafficanalyzer.in/blog/implement-swiping-between-tabs-with-viewpager-in-action-bar-using-sherlock-library/

+0

感謝您的回覆。但我沒有使用Sherlock。我正在使用支持庫android-actionbar-compat。 – Kameswari

+0

我剛纔提到了你可以用來實現你的效果的方式,並提供這個鏈接作爲例子,你也可以使用ViewPager和ActionBarCompat Support-Library,只要相信在Google上搜索:) –

0

你可以嘗試一些變通方法是這樣的...

首先設置setDisplayShowHomeEnabled(true)然後按照下述

actionBar.setDisplayShowHomeEnabled(true); 

     try{ 
      View homeIcon = findViewById(android.R.id.home); 
      ((View) homeIcon.getParent()).setVisibility(View.GONE); 
     } 
     catch(Exception e) 
     { 

     } 
相關問題