gpt4 book ai didi

android - 需要在 Android 的抽屉布局中使 ListView 垂直居中

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:19 26 4
gpt4 key购买 nike

我设计了一个抽屉布局,其中包含列表中的一些选项(基本上是菜单名称和图标)。但我无法在抽屉布局的垂直中心制作 ListView 。这是我的总 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);
}
}

它是这样来的:

enter image description here

谁能帮我把它放在中心。

谢谢..

最佳答案

您的 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);

关于android - 需要在 Android 的抽屉布局中使 ListView 垂直居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21098836/

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