2012-08-31 36 views
1

我先告訴你的佈局,我想獲得: enter image description here獲得這樣一個標籤式佈局類似的佈局(下劃線上活動活動)

有兩個部分,一個活躍的紅色下劃線與1DP線:如果用戶切換到第2部分,則第2部分帶下劃線,而第1部分不再有效。 請注意,我不想在標籤式活動中使用圖標,佈局必須像圖片一樣:只加下劃線文字。

此外,另一個黑色1dp線將頂部導航欄與底層內容分開。 你可以幫我嗎?我不一定需要標籤式的活動,即使只是線性佈局和形狀的組合也是好的!

回答

1

這與android sdk20。有兩種可能的類型,有或沒有滑動手勢(從一個標籤到另一個標籤)。

沒有刷卡:

 import android.app.ActionBar; 
     import android.app.FragmentTransaction; 
     import android.os.Bundle; 
     import android.support.v4.app.Fragment; 
     import android.support.v4.app.FragmentActivity; 
     import android.view.Gravity; 
     import android.view.LayoutInflater; 
     import android.view.Menu; 
     import android.view.View; 
     import android.view.ViewGroup; 
     import android.widget.TextView; 

    public class Tabs extends FragmentActivity implements ActionBar.TabListener { 

private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item"; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // For each of the sections in the app, add a tab to the action bar. 
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section1).setTabListener(this)); 
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section2).setTabListener(this)); 
    actionBar.addTab(actionBar.newTab().setText(R.string.title_section3).setTabListener(this)); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM)) { 
     getActionBar().setSelectedNavigationItem(
       savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM)); 
    } 
} 

@Override 
public void onSaveInstanceState(Bundle outState) { 
    outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, 
      getActionBar().getSelectedNavigationIndex()); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.tabs, menu); 
    return true; 
} 



@Override 
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, show the tab contents in the container 
    Fragment fragment = new DummySectionFragment(); 
    Bundle args = new Bundle(); 
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, tab.getPosition() + 1); 
    fragment.setArguments(args); 
    getSupportFragmentManager().beginTransaction() 
      .replace(R.id.container, fragment) 
      .commit(); 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

/** 
* A dummy fragment representing a section of the app, but that simply displays dummy text. 
*/ 
public static class DummySectionFragment extends Fragment { 
    public DummySectionFragment() { 
    } 

    public static final String ARG_SECTION_NUMBER = "section_number"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     TextView textView = new TextView(getActivity()); 
     textView.setGravity(Gravity.CENTER); 
     Bundle args = getArguments(); 
     textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); 
     return textView; 
    } 
} 
} 

這是選項卡+輕掃:

 import android.app.ActionBar; 
    import android.app.FragmentTransaction; 
    import android.content.Context; 
    import android.os.Bundle; 
    import android.support.v4.app.Fragment; 
    import android.support.v4.app.FragmentActivity; 
    import android.support.v4.app.FragmentManager; 
    import android.support.v4.app.FragmentPagerAdapter; 
    import android.support.v4.app.NavUtils; 
    import android.support.v4.view.ViewPager; 
    import android.view.Gravity; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.TextView; 

    public class TabsSwipe extends FragmentActivity implements ActionBar.TabListener { 

/** 
* The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the 
* sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will 
* keep every loaded fragment in memory. If this becomes too memory intensive, it may be best 
* to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
ViewPager mViewPager; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.tabs_swipe_new); 
    // Create the adapter that will return a fragment for each of the three primary sections 
    // of the app. 
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 

    // Set up the action bar. 
    final ActionBar actionBar = getActionBar(); 
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

    // Set up the ViewPager with the sections adapter. 
    mViewPager = (ViewPager) findViewById(R.id.pager); 
    mViewPager.setAdapter(mSectionsPagerAdapter); 

    // When swiping between different sections, select the corresponding tab. 
    // We can also use ActionBar.Tab#select() to do this if we have a reference to the 
    // Tab. 
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { 
     @Override 
     public void onPageSelected(int position) { 
      actionBar.setSelectedNavigationItem(position); 
     } 
    }); 

    // For each of the sections in the app, add a tab to the action bar. 
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) { 
     // Create a tab with text corresponding to the page title defined by the adapter. 
     // Also specify this Activity object, which implements the TabListener interface, as the 
     // listener for when this tab is selected. 
     actionBar.addTab(
       actionBar.newTab() 
         .setText(mSectionsPagerAdapter.getPageTitle(i)) 
         .setTabListener(this)); 
    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.tabs_swipe_new, menu); 
    return true; 
} 



@Override 
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

@Override 
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
    // When the given tab is selected, switch to the corresponding page in the ViewPager. 
    mViewPager.setCurrentItem(tab.getPosition()); 
} 

@Override 
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { 
} 

/** 
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary 
* sections of the app. 
*/ 
public class SectionsPagerAdapter extends FragmentPagerAdapter { 

    public SectionsPagerAdapter(FragmentManager fm) { 
     super(fm); 
    } 

    @Override 
    public Fragment getItem(int i) { 
     Fragment fragment = new DummySectionFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return 3; 
    } 

    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: return getString(R.string.title_section1).toUpperCase(); 
      case 1: return getString(R.string.title_section2).toUpperCase(); 
      case 2: return getString(R.string.title_section3).toUpperCase(); 
     } 
     return null; 
    } 
} 

/** 
* A dummy fragment representing a section of the app, but that simply displays dummy text. 
*/ 
public static class DummySectionFragment extends Fragment { 
    public DummySectionFragment() { 
    } 

    public static final String ARG_SECTION_NUMBER = "section_number"; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     TextView textView = new TextView(getActivity()); 
     textView.setGravity(Gravity.CENTER); 
     Bundle args = getArguments(); 
     textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER))); 
     return textView; 
    } 
} 
} 

要改變標籤的數量只是改變getCount將()方法,並使其返回2(代替3- )和getPageTitle()方法取消案例2:行(如果只需要兩個選項卡)。

您必須在Android 4.0或更高版本上運行此操作。

爲了使它在較低的android版本上運行,您將不得不使用ActionBarSherlock庫。你將不得不在你的項目中實現它(自己看看怎麼做),那麼你將不得不改變這些代碼。

public class TabsSwipe extends FragmentActivity implements ActionBar.TabListener { 

public class TabsSwipe extends SherlockFragmentActivity implements ActionBar.TabListener { 

然後:

- 刪除全部進口,並開始了進口的東西,當你有一個的choise總是選擇 「support.v4。」選項或「com.actionbarsherlock.app」。選項

-All getActionBar()的occurrency到getSupportActionBar()

-All getMenuInflater()的至getSupportMenuInflater()的occurrency

有一個DummySectionFragment類,這僅僅是一個示例片段,您必須構建自己的片段並更改活動類中的調用以匹配片段的名稱。

編輯:我忘了粘貼xml,你將不得不在佈局文件夾(裏面res)內創建。

標籤而不滑動手勢: tabs.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/container" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".Tabs" /> 

標籤與掃掠姿態: tabs_swipe_new.xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context=".TabsSwipe" />