如何以编程方式创建DrawerLayout

编程入门 行业动态 更新时间:2024-10-09 02:27:23
本文介绍了如何以编程方式创建DrawerLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想在我的主要活动中将此xml转换为java,输出是导航抽屉.我只需要这部分的帮助,其余部分我就完成了.

I would like to translate this xml to java in my main activity, the output is navigation drawer. I need help only in this part, I finished the remaining parts.

<android.support.v4.widget.DrawerLayout xmlns:android="schemas.android/apk/res/android" xmlns:tools="schemas.android/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <FrameLayout android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" > </FrameLayout> <ListView android:id="@+id/drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#FFF" android:choiceMode="singleChoice"/>

我想用Java代码在xml上面写,怎么做?

I would like to write above xml in java code, How ?

预先感谢

更新

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this); final FrameLayout fl = new FrameLayout(this); fl.setId(CONTENT_VIEW_ID); final ListView navList = new ListView (this); drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); drawer.addView(navList); setContentView(drawer);

在代码中自定义listview的布局宽度和布局重力不需要什么帮助.

I need little help to customise listview's layout width and layout gravity in code.

推荐答案

(由OP在问题编辑中回答.转换为社区Wiki答案.请参见没有答案的问题,但问题已在评论中解决(或在聊天中扩展))

(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )

OP写道:

我解决了,这里是完整的代码

I solved, here is the complete code

final DrawerLayout drawer = new android.support.v4.widget.DrawerLayout(this); final FrameLayout fl = new FrameLayout(this); fl.setId(CONTENT_VIEW_ID); final ListView navList = new ListView (this); DrawerLayout.LayoutParams lp = new DrawerLayout.LayoutParams( 100 , LinearLayout.LayoutParams.MATCH_PARENT); lp.gravity=Gravity.START; navList.setLayoutParams(lp); drawer.addView(fl, new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); drawer.addView(navList); setContentView(drawer);

更多推荐

如何以编程方式创建DrawerLayout

本文发布于:2023-11-28 11:15:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642177.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方式   DrawerLayout

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!