我想创建一个导航抽屉效果,而不是从右边滑出的抽屉。我创建的代码和我的代码在我的幻灯片菜单左侧时工作正常。这是我的java代码。
DrawerLayout drawerLayout;
View drawerView;
View drawerContent;
View mainContent;
ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerView = findViewById(R.id.drawer_view);
drawerContent = findViewById(R.id.drawer_content);
mainContent = findViewById(R.id.main_content);
toggle = new ActionBarDrawerToggle(
TestLayout.this,
drawerLayout,
R.drawable.ic_drawer,
R.string.u_drawer_call_center,
R.string.u_drawer_call_center
) {
@Override
public void onDrawerSlide(View drawer, float slideOffset)
{
super.onDrawerSlide(drawer, slideOffset);
drawerContent.setX(drawerView.getWidth() * (1 - slideOffset));
mainContent.setX(drawerView.getWidth() * slideOffset);
}
public void onDrawerClosed(View view) {
}
public void onDrawerOpened(View drawerView) {
}
};
drawerLayout.setDrawerListener(toggle);
drawerLayout.closeDrawer(drawerView);
drawerContent.setX(drawerView.getWidth());
}
} 正如我所说,我可以在我的幻灯片菜单左侧移动主要内容。但现在我想要幻灯片菜单右侧。我重写了我的xml代码,但我无法移动主要内容 这是我的xml代码
<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"
android:background="#000000">
<FrameLayout android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc" />
<LinearLayout android:id="@+id/drawer_view"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right">
<LinearLayout android:id="@+id/drawer_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
</LinearLayout>
</LinearLayout>
我也改变了源代码
drawerContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() * (1 - slideOffset)));
mainContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() * slideOffset));
但是这段代码无法正常工作。当我有幻灯片菜单右侧时,我不知道如何使用我的代码。 如果有人知道解决方案,请帮助我 谢谢大家
答案 0 :(得分:0)
除了更改layout_gravity
上的drawer_view
之外,您已经完成了必要的更改。在my original code中,否定drawerView.getWidth()
的每次出现。那就是:
toggle = new ActionBarDrawerToggle(...) {
...
@Override
public void onDrawerSlide(View drawer, float slideOffset) {
super.onDrawerSlide(drawer, slideOffset);
drawerContent.setX(-drawerView.getWidth() * (1 - slideOffset));
mainContent.setX(-drawerView.getWidth() * slideOffset);
...
}
};
...
drawerContent.setX(-drawerView.getWidth());
然后,您需要覆盖MainActivity
的{{1}}方法,如下所示:
onOptionsItemSelected()