2014-04-16 99 views
1

我在我的應用程序中添加了導航抽屜。一切正常,但現在我試圖添加一個簡單的textView菜單,並沒有成功。問題在於文本隱藏在actionBar本身之下。即使從頂部保證金50dp是不夠的。導航抽屜:文字隱藏在操作欄下方?

你有關於如何解決這個問題的提示嗎?

我的主要活動:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <!-- The main content view --> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/container" 
     android:fitsSystemWindows="true" 
     android:clipToPadding="false"/> 
    <!-- The navigation drawer --> 
    <FrameLayout 
     android:id="@+id/left_drawer" 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="left" 
     android:choiceMode="singleChoice" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" 
     android:background="@color/dark_brown"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:textSize="40dp" 
      android:textColor="#ffffff" 
      android:id="@+id/textView" 
      android:layout_marginTop="50dp" 
      android:layout_marginLeft="20dp" 
      android:gravity="center_vertical"/> 
    </FrameLayout> 
</android.support.v4.widget.DrawerLayout> 

而我的主要活動類:

public class MainActivity extends ActionBarActivity { 

    private DrawerLayout mDrawerLayout; 
    private ActionBarDrawerToggle mDrawerToggle; 
    private DatabaseHandler database; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     // Handle application side menu 
     sideMenu(); 

     // Set tint for android bar (only for KitKat version) 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
      SystemBarTintManager tintManager = new SystemBarTintManager(this); 
      tintManager.setStatusBarTintEnabled(true); 
      tintManager.setStatusBarTintResource(R.color.action_bar_bg); 
     } 

     // Create system objects 
     database = new DatabaseHandler(this); 
     final Statistics statistic = new Statistics(database); 

     // Create main fragment and point app to it 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .replace(R.id.container, new MainFragment(database, statistic)) 
        .commit(); 
     } 
    } 

    private void sideMenu() { 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     // host Activity 
       mDrawerLayout,   // DrawerLayout object 
       R.drawable.ic_drawer, // nav drawer icon to replace 'Up' caret 
       R.string.drawer_open,    // "open drawer" description 
       R.string.drawer_close   // "close drawer" description 
     ) { 
      // Called when a drawer has settled in a completely closed state. 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getSupportActionBar().setTitle(R.string.drawer_close); 
      } 

      // Called when a drawer has settled in a completely open state. 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getSupportActionBar().setTitle(R.string.drawer_open); 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     // Sync the toggle state after onRestoreInstanceState has occurred. 
     mDrawerToggle.syncState(); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     mDrawerToggle.onConfigurationChanged(newConfig); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Pass the event to ActionBarDrawerToggle, if it returns 
     // true, then it has handled the app icon touch event 
     return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     database.closeDatabase(); 
    } 
} 

回答

2

你想分配fitsSystemWindows和clipToPadding到抽屜式導航片段(最有可能的ListView控件)

像這樣 -

<ListView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:choiceMode="singleChoice" 
android:divider="@android:color/transparent" 
android:dividerHeight="0dp" 
android:background="#cccc" 

android:fitsSystemWindows="true" 
android:clipToPadding="false" 

/> 

但在你的情況下,你使用activity_main本身的FrameLayout來填充Drawer的內容。所以,你必須將這兩個屬性應用到的FrameLayout

<FrameLayout 
    android:id="@+id/left_drawer" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="left" 
    android:choiceMode="singleChoice" 
    android:divider="@android:color/transparent" 
    android:dividerHeight="0dp" 

    android:fitsSystemWindows="true" 
    android:clipToPadding="false" 

    android:background="@color/dark_brown"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:textSize="40dp" 
     android:textColor="#ffffff" 
     android:id="@+id/textView" 
     android:gravity="center_vertical"/> 
</FrameLayout> 

此外,通知,就不需要再下到你的TextView。

android:layout_marginTop="50dp" 
android:layout_marginLeft="20dp" 
+0

這一點以及內容區域中可能沒有'fitsSystemWindows =「true」'集合的視圖解決了它對我來說,謝謝。 – falstro