android布局顶部导航,顶部ActionBar上的Android导航抽屉

我正试图将导航抽屉放在操作栏上,当它向右滑动时,就像这个应用程序:

[删除]

这是我的主要活动的布局:

android:layout_width="fill_parent"

android:layout_height="fill_parent">

...

android:layout_gravity="start"

android:id="@id/navigation"

android:layout_width="@dimen/navigation_menu_width"

android:layout_height="fill_parent" />

stackoverflow上的其他一些问题类似于this question,但建议所有答案都使用滑动菜单库.但是这个应用程序他们仍然使用android.support.v4.widget.DrawerLayout并且他们成功了.不要问我怎么知道他们使用标准导航抽屉,但我确定.

非常感谢您的帮助.

解决方法:

您只需要删除窗口装饰视图的第一个子项,并将第一个子项添加到抽屉的内容视图中.之后,您只需将抽屉添加到窗口的装饰视图中.

以下是您执行此操作的一些详细步骤.

首先,创建一个名为“decor.xml”的xml或任何你喜欢的东西.只放入DrawerLayout和抽屉.下面的“FrameLayout”只是一个容器.我们将用它来包装您活动的内容.

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"/>

android:layout_gravity="start"

android:id="@id/navigation"

android:layout_width="@dimen/navigation_menu_width"

android:layout_height="fill_parent" />

然后删除主布局中的DrawerLayout.现在主要活动的布局应该是这样的

android:layout_width="fill_parent"

android:layout_height="fill_parent">

...

我们假设主要活动的布局名为“main.xml”.

在您的MainActivity中,写如下:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

// Inflate the "decor.xml"

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

// HACK: "steal" the first child of decor view

ViewGroup decor = (ViewGroup) getWindow().getDecorView();

View child = decor.getChildAt(0);

decor.removeView(child);

FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.

container.addView(child);

// Make the drawer replace the first child

decor.addView(drawer);

// Do what you want to do.......

}

现在你有一个可以在ActionBar上滑动的DrawerLayout.但您可能会发现状态栏覆盖了它.您可能需要向Drawer添加paddingTop才能解决问题.

标签:android,navigation,android-actionbar,drawer

来源: https://codeday.me/bug/20190918/1811581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值