gpt4 book ai didi

android - 没有操作栏的抽屉导航,android

转载 作者:行者123 更新时间:2023-11-30 02:04:17 24 4
gpt4 key购买 nike

我正在尝试实现没有任何操作栏的抽屉导航。我在主布局的顶部有一个小布局,如下所示 enter image description here

我希望当我单击按钮时,抽屉导航将出现在小(彩色)布局下。我试过一些例子,但抽屉总是这样 enter image description here

但我希望抽屉导航出现在小布局下而不是“从顶部”。我想要这样的东西: enter image description here

我怎样才能做到这一点?

我试过的示例的 xml 文件:

   <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>

主要 Activity 是:

  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

如果你有固定高度的布局,你可以设置从顶部到 ListView

的边距

Approach 2

使用此方法计算您的 RelativeLayout 的高度

然后

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();
}
});
}

然后使用它来将运行时边距设置为 ListView from top

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.setMargins(left, top, right, bottom);
imageView.setLayoutParams(lp);

关于android - 没有操作栏的抽屉导航,android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961326/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com