【问题标题】:Navigation drawer: text is hidden below actionbar?导航抽屉:文本隐藏在操作栏下方?
【发布时间】:2014-04-16 18:41:28
【问题描述】:

我在我的应用程序中添加了导航抽屉。一切正常,但现在我试图在菜单中添加一个简单的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();
    }
}

【问题讨论】:

    标签: android class navigation-drawer


    【解决方案1】:

    您希望将 fitSystemWindows 和 clipToPadding 分配给 Navigation Drawer 片段(很可能是 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 来填充抽屉内容。所以你必须将这两个属性应用到 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"
    

    【讨论】:

    • 这与fitsSystemWindows="true" 设置的内容区域中可能没有视图的事实一起为我解决了这个问题,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多