Pregunta Es necesario hacer que la vista de lista se centre verticalmente en Diseño de cajones en Android


He diseñado un diseño de cajón que contiene algunas opciones en una lista (básicamente, el nombre del menú y un icono). Pero no puedo hacer la vista de lista en el centro vertical del diseño del cajón. Aquí está mi diseño xml total:

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

Aquí está el código java de diseño de cajón:

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

Y aquí está el evento de clic para el elemento de la lista de cajones:

/**
 * 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);
    }
}

Viene así:

enter image description here

¿Alguien puede ayudarme a llegar al centro?

Gracias..


5
2018-01-13 18:38


origen


Respuestas:


su altura de ListView es match_parent, por lo que establecer layout_gravity en center_vertical no hará una diferencia, intente establecer la gravedad center_vertical en lugar de que el contenido de la vista de lista se centre verticalmente.

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

Esto debería funcionar pero si no prueba esto:

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

Cambio:

mDrawerLayout.closeDrawer(mDrawerList);

a:

mDrawerLayout.closeDrawer(Gravity.LEFT);

8
2018-01-13 18:49