Haz que la imagen aparezca en la mitad de la pantalla
Tengo problemas para hacer que mi imagen aparezca en la mitad de la pantalla. Aquí está mi archivo xml. Debe ocupar la mitad del tamaño de la pantalla y también el diseño lineal con la vista de texto debe estar en el pie de la imagen.
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/fond" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" android:layout_below="@+id/relativeLayout1" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentLeft="true" > <WebView android:id="@+id/webcontent" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginTop="151dp" /> </RelativeLayout> <ImageView android:id="@+id/image_actualitedetails" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/six_yamaha" /> <LinearLayout android:id="@+id/linearvideo" android:layout_width="fill_parent" android:layout_height="65dp" android:layout_alignBottom="@+id/image_actualitedetails" android:layout_alignParentLeft="true" android:background="#88FFFFFF" android:gravity="center" android:paddingLeft="@dimen/fourdp" android:paddingRight="@dimen/fourdp" > <TextView android:id="@+id/text_actualites" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textColor="@color/tabDark" android:textStyle="bold" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent" android:layout_height="75dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@drawable/header" > <Button android:id="@+id/boutton_retour" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:layout_marginLeft="14dp" android:background="@drawable/back" /> </RelativeLayout>
- Actualización de layoutParams no funciona
- Zoom y arrastrar una imagen en android
- Cómo cambiar el origen de ImageView en android
- Animación de una imagen con ImageView
- Alpha-gradient en Android
- ¿Ajustar la imagen grande en imagen pequeña con tan buena calidad como la imagen original?
- Inserción de contenido con escala escalable personalizada
- Android: Cargar imagen desde la URL web
- Problema con CoordinatorLayout y ImageView que ajusta el ancho mientras se desplaza
- OnTouch dando extraños puntos de contacto Android
- Transformar / Animar vista en Activity1 para ver en Activity2
- 'Falta el atributo contentDescription en la imagen' en XML
- Cómo voltear ImageView en Android?
Utilice LinearyLayout para ajustar su ImageView y otra vista invisible y establecer layout_weight a 0,5, entonces el ImageView debe ser la mitad del tamaño.
Aquí hay un ejemplo para mostrar cómo configurar la mitad de la imagen del tamaño de la pantalla
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5" android:background="#ff0000" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/your_image" android:adjustViewBounds="true" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="0.5" android:background="#00ff00" /> </LinearLayout>
Y este es el resultado
Si desea poner su imagen en el centro de la pantalla, puede utilizar la misma idea y establecer el peso adecuado para lograr su objetivo. ¡Buena suerte!
Sólo si alguien quiere la versión de paisaje es casi lo mismo 🙂
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:baselineAligned="true" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5" android:background="#ff0000" > <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/adele" /> </RelativeLayout> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.5" android:background="#00ff00" /> </LinearLayout>
Primero necesitará obtener las dimensiones de la pantalla en tiempo de ejecución
Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); windowWidth = size.x; windowHeight = size.y;
A continuación, puede seguir adelante y establecer los parámetros height y width de imageView en la mitad de esos valores
int orgWidth = yourImageView.getDrawable().getIntrinsicWidth(); int orgHeight = yourImageView.getDrawable().getIntrinsicHeight(); //Use RelativeLayout.LayoutParams if your parent is a RelativeLayout LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( windowHeight / 2, windowWidth / 2); yourImageView.setLayoutParams(params); yourImageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
Usted tiene que poner su imageview dentro de un LinearLayout con la alineación horizontal, así que usted puede utilizar
android:weight = 0.5// this attribute for the image view will force the image to fill half of the screen, counting of course with the linear layout as the top parent layout(filling the hole window)
- Android Studio, Gradle, OpenCV y NDK
- Linux Ubuntu Android SDK manager no está iniciando – android: java: no encontrado