2013-07-29 290 views
3

截圖
enter image description here導航抽屜重疊過的ActionBar導航選項卡內容

由於它是在上述圖像中可見,當導航抽屜滑動打開時,存在於標籤的內容重疊在它

代碼
Fragment2.java - 即在Tab

中所示的片段
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class Fragment2 extends Fragment{ 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View v = inflater.inflate(R.layout.fragment2, null); 
     v.setTag("artist"); 
     return v; 
    } 
} 


fragment2.xml - 通過上述片段

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="There" 
     android:layout_marginTop="100dp" 
     android:layout_gravity="center_horizontal" 
    /> 

    <ToggleButton 
     android:id="@+id/toggleButton1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="ToggleButton" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Large Text" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</LinearLayout> 


MainActivity.java --Containing在ActionBar和導航抽屜充氣的XML(此代碼是從Google's ActionBar Guide

import java.util.List; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.v4.app.ActionBarDrawerToggle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.MenuItemCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBar; 
import android.support.v7.app.ActionBar.Tab; 
import android.support.v7.app.ActionBarActivity; 
import android.support.v7.widget.SearchView; 
import android.support.v7.widget.SearchView.OnQueryTextListener; 
import android.support.v7.widget.ShareActionProvider; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.ArrayAdapter; 
import android.widget.BaseAdapter; 
import android.widget.ImageView; 
import android.widget.ListView; 
import android.widget.TextView; 
import android.widget.Toast; 


public class MainActivity extends ActionBarActivity implements OnQueryTextListener { 

    private ActionBar actionBar; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private ShareActionProvider mShareActionProvider; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     tab(); // Code related to the Tabs 
    } 

void tab(){ 
     actionBar = getSupportActionBar(); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); 

     Tab tabA = actionBar.newTab(); 
     tabA.setText("Articles"); 
    tabA.setTabListener(new TabListener(
      this, "artist", Fragment1.class)); 
     actionBar.addTab(tabA); 

     Tab tabB = actionBar.newTab(); 
     tabB.setText("Videos"); 
     tabB.setTabListener(new TabListener(
       this, "album", Fragment2.class)); 
     actionBar.addTab(tabB); 



     String[] mPlanetTitles; 
     DrawerLayout mDrawerLayout; 
      ListView mDrawerList; 
    mPlanetTitles = getResources().getStringArray(R.array.action_list); 
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     // Set the adapter for the list view 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mPlanetTitles)); 
     // Set the list's click listener 

     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.abc_ic_ab_back_holo_dark, /* nav drawer icon to replace 'Up' caret */ 
       R.string.abc_action_bar_home_description, /* "open drawer" description */ 
       R.string.abc_activity_chooser_view_see_all /* "close drawer" description */ 
       ) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       getSupportActionBar().setTitle("Later"); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       getSupportActionBar().setTitle("Menu"); 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     //respond to menu item selection 

     if (mDrawerToggle.onOptionsItemSelected(item)) { 
       return true; 
      } 

    switch (item.getItemId()) { 
    case R.id.show: finish(); 
    return true; 
    case R.id.close: finish(); 
    return true; 

    case R.id.remove: finish(); 
     return true; 
    case R.id.update: finish(); 
} 
    return true; 
    } 

@Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 

     MenuItem searchItem = menu.findItem(R.id.action_search); 
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
     searchView.setOnQueryTextListener(this); 
     MenuItem shareItem = menu.findItem(R.id.action_share); 
     mShareActionProvider = (ShareActionProvider) 
       MenuItemCompat.getActionProvider(shareItem); 
     mShareActionProvider.setShareIntent(getDefaultIntent()); 
     return true; 
    } 

private Intent getDefaultIntent() { 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("*/*"); 
     return intent; 
    } 
@Override 
public boolean onQueryTextChange(String arg0) { 
    // TODO Auto-generated method stub 
    return false; 
} 
@Override 
public boolean onQueryTextSubmit(String arg0) { 
    // TODO Auto-generated method stub 
    Toast.makeText(this, "You searched for : " +arg0, Toast.LENGTH_LONG).show(); 
    InputMethodManager imm = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE); 
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); 
    return false; 
} 


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

    public TabListener(Activity activity, String tag, Class<T> clz) { 
     mActivity = activity; 
     mTag = tag; 
     mClass = clz; 
    } 


    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. 

    } 
} 
} 


activity_main.xml中 - 包含DrawerLayout

<android.support.v4.widget.DrawerLayout 
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; 
    android:id=&quot;@+id/drawer_layout&quot; 
    android:layout_width=&quot;match_parent&quot; 
    android:layout_height=&quot;match_parent&quot;> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
      > 
    </ListView> 

<ListView android:id="@+id/left_drawer" 
     android:layout_width="240dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:choiceMode="singleChoice" 

     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 

     android:background="#111"/> 
</android.support.v4.widget.DrawerLayout> 


這是我的代碼有問題的XML,我錯過了或忽略了什麼?任何反饋將有很大的幫助

+0

嗨Prayag,是你能解決這個問題?我面臨着和你一樣的問題。 –

回答

1

這是一點點的解決方法,但由於ActionBar選項卡在21 API中已棄用,因此可以在佈局中使用選項卡(如TabHost)。在這種情況下,抽屜會顯示在標籤上方。

代碼:

TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    tabHost.setup(); 

    TabHost.TabSpec tabSpec; 
    tabSpec = tabHost.newTabSpec("1"); 
    tabSpec.setIndicator("Tab"); 
    tabSpec.setContent(R.id.list_fragment); 
    tabHost.addTab(tabSpec); 

    tabHost.setOnTabChangedListener(listener); 

,並將其添加在佈局:

<TabHost 
    a:id="@android:id/tabhost" 
    a:layout_width="match_parent" 
    a:layout_height="match_parent"> 
    <LinearLayout 
     a:layout_width="match_parent" 
     a:layout_height="match_parent" 
     a:orientation="vertical"> 
     <TabWidget 
      a:id="@android:id/tabs" 
      a:layout_width="match_parent" 
      a:layout_height="wrap_content"> 
     </TabWidget> 
     <FrameLayout 
      a:id="@android:id/tabcontent" 
      a:layout_width="match_parent"> 

      </FrameLayout> 
    </LinearLayout> 
</TabHost> 
0

我認爲你會更好使用像ViewPagerIndicator圖書館。 ActionBar被識別爲裝飾並顯示在窗口的頂部,並且DrawerLayout被繪製爲窗口中的對象。

不知道他們是否打算在未來版本中修復它,但ViewPagerIndicator非常容易設置和自定義。