Android导航抽屉不会填充操作栏空间

时间:2016-03-10 04:10:12

标签: android listview android-studio android-actionbar navigation-drawer

我正在开发一个使用侧面导航抽屉的应用程序。我试图让导航抽屉滑动,操作栏向右移动,我完成了。但是,当操作栏向右移动时,导航抽屉中的ListView不会填满空格。它只留下一个空的空白灰色空间。请帮忙。

以下是我的代码:

myActivity.xml:

   <android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".currentJob"
    xmlns:tools="http://schemas.android.com/tools">

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="16dp"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:orientation="vertical"
            android:weightSum="1">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Random Text"
            android:id="@+id/lbl_title"
            android:gravity="center_horizontal"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textStyle="bold"
            android:textSize="@dimen/_20sdp"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>
    </ScrollView>

    <ListView

        android:id="@+id/navList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="left|start"
        android:background="#ffeeeeee"
        android:fitsSystemWindows="true" />

    </android.support.v4.widget.DrawerLayout>

MyActivity.java

        protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_current_job);

        //setup list header
        listHeaderView = View.inflate(this,R.layout.menuheaderview, null);

        //setup drawer
        mDrawerList = (ListView)findViewById(R.id.navList);
        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
        double width=(getResources().getDisplayMetrics().widthPixels/100.00)*75;    //drawer layout width in percentage

        int w = (int)width;

        DrawerLayout.LayoutParams params =                     (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
        params.width = w;

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_drawer);

        mDrawerList.setLayoutParams(params);
        addDrawerItems();
        setupDrawer();
    }

addDrawerItems function

private void addDrawerItems() {

    //add items operations
    mAdapter = new MyCustomAdapter(menu,this);
    mDrawerList.setAdapter(mAdapter);
    mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener()          {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Toast.makeText(myActivity.this, "You selected ", Toast.LENGTH_SHORT).show();
        }
    });
}

setupDrawer函数

private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
        R.string.drawer_open, R.string.drawer_close) {

    /** Called when a drawer has settled in a completely open state. */
    public void onDrawerOpened(View drawerView) {

        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    /** Called when a drawer has settled in a completely closed state. */
    public void onDrawerClosed(View view) {

        invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
    }

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);

        float xPositionOpenDrawer = mDrawerList.getWidth();
        float xPositionWindowContent = (slideOffset * xPositionOpenDrawer);
        getActionBarView().setX(xPositionWindowContent);
    }
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerToggle.setHomeAsUpIndicator(R.drawable.icon_drawer);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}

getActionBarView函数

public View getActionBarView() {

    Window window = this.getWindow();
    final View decorView = window.getDecorView();
    final String packageName =  getPackageName();
    final int resId =        this.getResources().getIdentifier("action_bar_container", "id", packageName);
    final View actionBarView = decorView.findViewById(resId);
    return actionBarView;
}

请帮助我,我真的尽我所能,但仍无法找到任何解决方案。

NavigationDrawerProblem

1 个答案:

答案 0 :(得分:0)

如果您想要这种效果,请考虑使用工具栏而不是ActionBar。使用工具栏对象支持作为操作栏。它可以灵活地将工具栏放置在布局文件中的任何位置,就像布局中的任何其他视图一样。