2012-08-31 1 views
1

(활성 활동에 밑줄) 확보 : enter image description here이 같은 탭 레이아웃과 같은 레이아웃은 내가 먼저 내가 얻을 싶습니다 레이아웃 보여

은 두 개의 섹션이 있으며, 활성 하나는 빨간색이 1DP 라인 밑줄 : 사용자가 섹션 2로 전환하면 섹션 2는 밑줄이 그어져 있지만 섹션 1은 더 이상 강조 표시되지 않습니다. 탭으로 표시된 활동과 같은 아이콘을 원하지 않습니다. 레이아웃은 그림과 같아야합니다. 바로 밑줄이 그어진 텍스트입니다.

검은 색 1dp 행은 기본 탐색 모음과 기본 콘텐츠를 구분합니다. 나를 도울 수 있습니까? 선형 레이아웃과 모양을 조합하여도 탭 작업이 반드시 필요한 것은 아닙니다.

답변

1

이것은 android sdk20과 함께 제공됩니다. 스 와이프 제스처가 있거나없는 두 가지 유형 (한 탭에서 다른 탭으로 이동)이 있습니다.

 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 만들려면 :이 탭 + 슬쩍입니다

 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; 
    } 
} 
} 

: 슬쩍없이

) 및 getPageTitle() 메소드에서 대소 문자를 2 : 행 취소하십시오 (두 개의 탭만 원할 경우).

Android 4.0 이상에서 실행해야합니다.

낮은 Android 버전에서 실행하려면 ActionBarSherlock 라이브러리를 사용해야합니다. 프로젝트에서이 코드를 구현해야합니다 (직접 수행하는 방법 참조). 그런 다음이 코드에서 변경해야합니다.

- 삭제 모든 수입과 당신이의 choise는 항상 선택이있을 때, 물건을 가져 오기 시작 :

public class TabsSwipe extends FragmentActivity implements ActionBar.TabListener { 

그런

public class TabsSwipe extends SherlockFragmentActivity implements ActionBar.TabListener { 

에 "support.v4을." 옵션 또는 "com.actionbarsherlock.app" getSupportMenuInflater()에 getMenuInflater()의 occurrency - 모든 getSupportActionBar()

에 getActionBar()의 occurrency - 모든 옵션

DummySectionFragment 클래스가있다

, 그건 단지 예입니다 조각을 만들려면 자신의 단편을 만들고 단편의 이름과 일치하도록 활동 클래스의 호출을 변경해야합니다.

편집 : 레이아웃 폴더 (내부 해상도) 안에 작성해야하는 XML을 붙여 넣는 것을 잊어 버렸습니다. 슬쩍 제스처없이

탭 : 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" /> 
관련 문제