Android establece la lista de cajones de navegación para abrir la mitad exacta de la pantalla para toda la pantalla del dispositivo


Quiero abrir la lista de dibujo a la mitad de la pantalla para todos los dispositivos diferentes. probé valores codificados para layout_margineleft 200dp o 100dp a la lista de dibujo. pero no funciona en todos los dispositivos, es diferente de un dispositivo a otro. entonces, ¿cómo puedo mantener exactamente la mitad de la pantalla para drawerlist? también probé varias funciones como setLeft (int), etc.pero algunos de ellos no funciona debido a que uso la versión mínima 8. Así que por favor ayúdame. gracias de antemano.

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);

Xml para que:

<?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"
  android:background="@color/setting_background" >

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

 android:layout_width="match_parent"
 android:layout_height="fill_parent"
 android:layout_margin="@dimen/top_news_linear_margin"
 android:background="@color/news_list_divider_color"
 android:orientation="vertical"
android:padding="@dimen/top_news_linear_padding" >

</RelativeLayout>

<ListView
 android:id="@+id/drawer_list"
 android:layout_width="wrap_content"
 android:layout_height="fill_parent"
 android:layout_gravity="right"
 android:layout_alignParentTop="true"
 android:background="@color/setting_background"
 android:cacheColorHint="@android:color/transparent"
 android:choiceMode="singleChoice"
 android:divider="@color/news_list_divider_color"
 android:dividerHeight="@dimen/news_list_divider_height"
 />

</android.support.v4.widget.DrawerLayout>
Author: Mayur Raval, 2014-01-10

5 answers

Establezca el ancho de ListView dinámicamente...

    mDrawerLayout = (DrawerLayout) view.findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) view.findViewById(R.id.top_sectionlist);

    int width = getResources().getDisplayMetrics().widthPixels/2;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) mDrawerList.getLayoutParams();
    params.width = width;
    mDrawerList.setLayoutParams(params);
 68
Author: Gopal Gopi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-10 07:08:40

Debe calcular manualmente el tamaño de la pantalla y establecer el ancho dinámico en el diseño del cajón en tiempo de ejecución, aquí se muestra cómo obtener el Ancho y la altura de la pantalla del dispositivo en tiempo de ejecución

Usando este código puede obtener el Ancho y Alto de la pantalla de tiempo de ejecución

DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;

Ahora, necesitas dividir el ancho entre 2

int newWidth=width/2;

Ahora establezca width en ListView de esta manera

drawer_list.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,newWidth));

Esto funcionará para todos los dispositivos y dará un tamaño uniforme en todos.

 7
Author: Akhil Jain,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-01-10 07:06:48

Aquí está mi solución para otros que necesitan varios porcentajes para mostrar allí cajón de navegación.

Aquí, uso el cajón de navegación derecho y muestro el 15% de la pantalla de inicio cuando el cajón se mueve a la izquierda.

  // convert your offset percent (here 15% ) into decimal by dividing 100 
    float offset = .15f * getResources().getDisplayMetrics().widthPixels ;
    float width = getResources().getDisplayMetrics().widthPixels - offset;
    DrawerLayout.LayoutParams params = (android.support.v4.widget.DrawerLayout.LayoutParams) rightNavigationView.getLayoutParams();
    params.width = (int) width;
    rightNavigationView.setLayoutParams(params);
 3
Author: Shihab Uddin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-27 15:42:54

Usando la nueva Biblioteca de Diseño de Soporte de Android, puede hacer uso de android.support.percent.PercentRelativeLayout para lograr esto fácilmente. Esta es mi solución.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginRight="-64dp"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginRight="64dp"/>

    <android.support.percent.PercentRelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        >

        <android.support.design.widget.NavigationView
            android:id="@+id/nav_view"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:fitsSystemWindows="true"
            app:layout_widthPercent="50%"
            app:headerLayout="@layout/nav_header_dashboard"
            app:menu="@menu/menu_drawer_activity_dashboard"/>

    </android.support.percent.PercentRelativeLayout>

</android.support.v4.widget.DrawerLayout>

Si estás preguntando por qué hay un android:layout_marginRight="-64dp" y android:layout_marginRight="-64dp" en el DrawerLayout y el contenido. Es debido al margen fijo de 64 dp que está codificado directamente en el DrawerLayout que no le permitiría usar un ancho completo del cajón. Más sobre eso aquí

 1
Author: gentra,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:17:36
 int currentVerion = Build.VERSION.SDK_INT;
 int width= 0;
 Display display = getWindowManager().getDefaultDisplay();
 Point size = new Point();

    if(currentVerion >= Build.VERSION_CODES.BASE) {
        display.getSize(size);
        width = size.x;
    }
    else
   {
       width = display.getWidth();
   }

((ListView) view.findViewById(R.id.top_sectionlist)).getLayoutParams().width =width/2;
 0
Author: Ahmad Moussa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-01-06 05:02:57