我设计了一个抽屉布局,其中包含一些列表选项(基本上是菜单名称和图标)。但我无法在抽屉布局的垂直中心制作列表视图。这是我的总xml布局:
<?xml version="1.0" encoding="utf-8"?>
<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" >
<!-- The main content view -->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="@drawable/pink_header"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="@+id/tot_detail_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Talk of Town Details"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_toRightOf="@id/feature_detail_title"
android:gravity="right"
android:orientation="horizontal" >
<Button
android:id="@+id/drawer_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_drawer"
android:visibility="visible" />
</LinearLayout>
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tot_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:textColor="#222222"
android:textSize="18sp"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="@+id/image_tot_detail"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="@drawable/image_frame"
android:padding="1dip"
android:scaleType="fitXY" />
<TextView
android:id="@+id/tot_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_loc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:textColor="#222222"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_Desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:text="Description:"
android:textColor="#0C7DAF"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="@+id/tot_Desc_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:textColor="#222222"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
</LinearLayout>
</ScrollView>
</LinearLayout>
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="fill_parent"
>
</FrameLayout>
<!-- The navigation drawer -->
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:choiceMode="singleChoice"
android:background="#343434"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer"
android:divider="#ffffff"
/>
</android.support.v4.widget.DrawerLayout>
这是抽屉布局java代码:
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Photos
navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
if(strTableBook.equals("yes"))
{
// Table Booking
navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1), true, "22"));
}
if(strGuestList.equals("yes"))
{
// Guest List
navDrawerItems.add(new NavDrawerItem(navMenuTitles[4], navMenuIcons.getResourceId(4, -1)));
}
if(strBuyNow.equals("yes"))
{
//Buy now
navDrawerItems.add(new NavDrawerItem(navMenuTitles[5], navMenuIcons.getResourceId(5, -1), true, "50+"));
}
Log.d("Testing", "here is 11111");
// Recycle the typed array
navMenuIcons.recycle();
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
Log.d("Testing", "here is 22222");
// enabling action bar app icon and behaving it as toggle button
// getActionBar().setDisplayHomeAsUpEnabled(true);
// getActionBar().setHomeButtonEnabled(true);
Log.d("Testing", "here is 33333");
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
)
{
public void onDrawerClosed(View view) {
// getActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
// getActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
Log.d("Testing", "here is 444444");
mDrawerLayout.setDrawerListener(mDrawerToggle);
这是抽屉列表项的点击事件:
/**
* Slide menu item click listener
* */
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
displayView(position);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
它是这样的:
任何人都可以帮我把它放在中心位置。
谢谢..
答案 0 :(得分:8)
你的ListView高度是match_parent,所以将layout_gravity设置为center_vertical不会有所作为尝试设置gravity center_vertical,这将使listview内容垂直居中。
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="fill_parent"
android:layout_gravity="left|center_vertical"
android:gravity="center_vertical"
android:choiceMode="singleChoice"
android:background="#343434"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer"
android:divider="#ffffff"
/>
这应该有效,但如果不能尝试:
<LinearLayout
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left"
android:background="#343434" >
<ListView
android:id="@+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#343434"
android:choiceMode="singleChoice"
android:divider="#ffffff"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector_drawer" />
</LinearLayout>
变化:
mDrawerLayout.closeDrawer(mDrawerList);
为:
mDrawerLayout.closeDrawer(Gravity.LEFT);