Pregunta

Una de nuestras opiniones tiene ScrollView como su diseño raíz.Cuando se gira el dispositivo y onConfigurationChanged() se llama, nos gustaría poder obtener el ScrollViewEl nuevo ancho/alto.Nuestro código se ve así:

@Override
public void onConfigurationChanged(Configuration newConfig) {
    Log.d(TAG, "Width: '" + findViewById(R.id.scrollview).getWidth() + "'");
    Log.d(TAG, "Height: '" + findViewById(R.id.scrollview).getHeight() + "'");

    super.onConfigurationChanged(newConfig);

    Log.d(TAG, "Width: '" + findViewById(R.id.scrollview).getWidth() + "'");
    Log.d(TAG, "Height: '" + findViewById(R.id.scrollview).getHeight() + "'");
}

La sección relevante de nuestro AndroidManifest.xml se ve así:

<activity android:name=".SomeActivity"
    android:configChanges="keyboardHidden|orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>

Y finalmente, la parte relevante de nuestro diseño se ve así:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollview"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    >
    <LinearLayout android:id="@+id/container"
        android:orientation="vertical"
        android:layout_height="fill_parent"
        android:minHeight="200dip"
        android:layout_width="fill_parent"
        >

En nuestro Droid, esperábamos ver que el ancho de ScrollView aumentara a 854 cuando se cambiaba a horizontal, y a 480 cuando se volvía a cambiar a vertical (y la altura hacía el cambio equivalente, menos la barra de menú).Sin embargo, estamos viendo lo contrario.Aquí está nuestro LogCat:

// Switching to landscape:
03-26 11:26:16.490: DEBUG/ourtag(17245): Width: '480'  // Before super
03-26 11:26:16.490: DEBUG/ourtag(17245): Height: '778' // Before super
03-26 11:26:16.529: DEBUG/ourtag(17245): Width: '480'  // After super
03-26 11:26:16.536: DEBUG/ourtag(17245): Height: '778' // After super

// Switching to portrait:
03-26 11:26:28.724: DEBUG/ourtag(17245): Width: '854'  // Before super
03-26 11:26:28.740: DEBUG/ourtag(17245): Height: '404' // Before super
03-26 11:26:28.740: DEBUG/ourtag(17245): Width: '854'  // After super
03-26 11:26:28.740: DEBUG/ourtag(17245): Height: '404' // After super

Claramente, obtenemos las dimensiones verticales cuando cambiamos a horizontal y las dimensiones horizontales cuando cambiamos a vertical.¿Hay algo que estemos haciendo mal?Podríamos hackear y resolver esto, pero siento que nos falta una solución simple.

¿Fue útil?

Solución

Al observar su reputación, no estoy seguro de si alguna vez no se enteró de las columnas de búsqueda, como se explica en este el artículo de MSDN están haciendo exactamente lo que quiere, pero solo la diferencia está jugando con el grupode bananos y su con herramientas;)

espero que también ayude :)

editar

Desde su búsqueda de XSLTLISTVIGHTWEBPART puede usar las combinaciones de Caml y las proyecciones como se explica en el libro SharePoint 2010 como plataforma de desarrollo

Otros consejos

Para aquellos que buscan una descripción más detallada de la solución:Puedes usar el ViewTreeObserver de su punto de vista y registrar un OnGlobalLayoutListener.

@Override
public void onConfigurationChanged(Configuration newConfiguration) {
    super.onConfigurationChanged(newConfiguration);
    final View view = findViewById(R.id.scrollview);

    ViewTreeObserver observer = view.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            Log.v(TAG,
                    String.format("new width=%d; new height=%d", view.getWidth(),
                            view.getHeight()));
            view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        }
    });
}

When onGlobalLayout called it's not sure that the view has been resized accordingly to the new layout orientation, so for me only the below solution worked properly:

@Override
public void onConfigurationChanged(Configuration newConfig) {
final int oldHeight = mView.getHeight();
final int oldWidth = mView.getWidth();

mView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mView.getHeight() != oldHeight && mView.getWidth() != oldWidth) {
                mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //mView now has the correct dimensions, continue with your stuff
            }
        }
    });
    super.onConfigurationChanged(newConfig);}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top