导航抽屉里,连动作条,机器人 [英] Navigation drawer without actionbar, android
问题描述
我想实现导航抽屉没有任何动作条。
我在主要布局的顶部有一个小的布局,看起来像下面
我想,当我会按一下按钮抽屉式导航栏将根据小(彩色)的布局出现。
我曾尝试用一些例子,但抽屉里总是出现这样的
但我想,导航抽屉将根据小版面,而不是从顶部出现。
我想是这样的:
我怎样才能做到这一点?
我试过的例子的XML文件:
< android.support.v4.widget.DrawerLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / drawer_layout
机器人:layout_width =match_parent
机器人:layout_height =match_parent> <! - 布局中的第一个孩子是主要活动的用户界面 - > < RelativeLayout的
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=#FFFFFFFF
机器人:paddingBottom会=@扪/ activity_vertical_margin
机器人:paddingLeft =@扪/ activity_horizontal_margin
机器人:paddingRight =@扪/ activity_horizontal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
工具:上下文=MainActivity。> < RelativeLayout的
机器人:ID =@ + ID /动作条
机器人:layout_width =match_parent
机器人:layout_height =50dp
机器人:layout_alignParentTop =真
机器人:背景=#0f9d58>
< / RelativeLayout的> < RelativeLayout的
机器人:ID =@ + ID /日程地址搜索Maincontent
机器人:layout_width =match_parent
机器人:layout_height =50dp
机器人:layout_below =@ ID /动作条
机器人:背景=#0f9d58> <按钮
机器人:ID =@ + ID / actionBarButton
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=点击/> <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerHorizontal =真
机器人:layout_marginTop =100dp
机器人:比重=中心
机器人:文字=神圣的操作系统,Batdroid!
机器人:TEXTSIZE =24sp/> < ImageView的
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentBottom =真
机器人:layout_centerHorizontal =真/>
< / RelativeLayout的>
< / RelativeLayout的> <! - 侧导航抽屉UI - > < ListView控件
机器人:ID =@ + ID / navList
机器人:layout_width =200dp
机器人:layout_height =match_parent
机器人:layout_below =@ ID /动作条
机器人:layout_gravity =左|启动
机器人:背景=#ffeeeeee/>< /android.support.v4.widget.DrawerLayout>
和的主要活动是:
公共类MainActivity延伸活动{ 私人的ListView mDrawerList;
私人ArrayAdapter<串GT; mAdapter;
按钮actionBarButton;
布尔buttonStateOpen; @覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main); buttonStateOpen = FALSE; 最后DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout); mDrawerList =(ListView控件)findViewById(R.id.navList); addDrawerItems();
actionBarButton =(按钮)findViewById(R.id.actionBarButton);
actionBarButton.setOnClickListener(新OnClickListener(){ @覆盖
公共无效的onClick(查看为arg0){
// TODO自动生成方法存根
如果(buttonStateOpen == FALSE)
{
drawer.openDrawer(Gravity.LEFT);
buttonStateOpen = TRUE;
}
否则,如果(buttonStateOpen ==真)
{
drawer.closeDrawer(Gravity.LEFT);
buttonStateOpen = FALSE;
}
}
});
mDrawerList.setOnItemClickListener(新AdapterView.OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
长ARG3){
// TODO自动生成方法存根
Toast.makeText(MainActivity.this,Toast.LENGTH_SHORT时间升级!).show();
}
});
} 私人无效addDrawerItems(){
的String [] = osArray {机器人,iOS版,窗口,OS X,Linux的};
mAdapter =新ArrayAdapter<串GT;(这一点,android.R.layout.simple_list_item_1,osArray);
mDrawerList.setAdapter(mAdapter);
}}
方法1
块引用>如果你有固定布局的高度,你可以设置太大的边距为
的ListView
方法2
块引用>使用这个方法来计算的高度,你的
RelativeLayout的
然后
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
如果(viewTreeObserver.isAlive()){
viewTreeObserver.addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
@覆盖
公共无效onGlobalLayout(){
。view.getViewTreeObserver()removeGlobalOnLayoutListener(本);
viewWidth = view.getWidth();
viewHeight = view.getHeight();
}
});
}然后用它来运行时距设置为
的ListView
从上LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(左,上,右,下);
imageView.setLayoutParams(LP);I am trying to implement navigation drawer without any actionbar. I have a small layout at the top of main layout and it looks like below
I want that when i will click the button the navigation drawer will appear under the small(colored) layout. I have tried with some example but the drawer always appear like this
But i want that the navigation drawer will appear under the small layout instead of "from the top" . I want something like this:
How can i achieve that??
My tried example's xml file:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- The first child in the layout is for the main Activity UI --> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ffffffff" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RelativeLayout android:id="@+id/actionBar" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:background="#0f9d58" > </RelativeLayout> <RelativeLayout android:id="@+id/mainContent" android:layout_width="match_parent" android:layout_height="50dp" android:layout_below="@id/actionBar" android:background="#0f9d58" > <Button android:id="@+id/actionBarButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="100dp" android:gravity="center" android:text="Holy Operating Systems, Batdroid!" android:textSize="24sp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> </RelativeLayout> <!-- Side navigation drawer UI --> <ListView android:id="@+id/navList" android:layout_width="200dp" android:layout_height="match_parent" android:layout_below="@id/actionBar" android:layout_gravity="left|start" android:background="#ffeeeeee" /> </android.support.v4.widget.DrawerLayout>
And the main activity was :
public class MainActivity extends Activity { private ListView mDrawerList; private ArrayAdapter<String> mAdapter; Button actionBarButton; Boolean buttonStateOpen; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); buttonStateOpen=false; final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout); mDrawerList = (ListView)findViewById(R.id.navList); addDrawerItems(); actionBarButton=(Button) findViewById(R.id.actionBarButton); actionBarButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(buttonStateOpen==false) { drawer.openDrawer(Gravity.LEFT); buttonStateOpen=true; } else if(buttonStateOpen==true) { drawer.closeDrawer(Gravity.LEFT); buttonStateOpen=false; } } }); mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show(); } }); } private void addDrawerItems() { String[] osArray = { "Android", "iOS", "Windows", "OS X", "Linux" }; mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray); mDrawerList.setAdapter(mAdapter); } }
解决方案Approach 1
if You have Fixed height of your layout the you can set that much of margin from top to
ListView
Approach 2
use this method to calculate the height of your
RelativeLayout
then
ViewTreeObserver viewTreeObserver = view.getViewTreeObserver(); if (viewTreeObserver.isAlive()) { viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.getViewTreeObserver().removeGlobalOnLayoutListener(this); viewWidth = view.getWidth(); viewHeight = view.getHeight(); } }); }
then use this to set Runtime margin to
ListView
from topLinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.setMargins(left, top, right, bottom); imageView.setLayoutParams(lp);
这篇关于导航抽屉里,连动作条,机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!