Mam wyswietlany obrazek prze ImageView chciałbym aby jego wielkość była zależna od wielkości ekranu. Da się to ogarnąć przy pomocy xml? lub inny sposób?
[Android] Dopasowanie wielkości obrazka do wielkości ekranu
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Kraków
- Postów: 834
Za pomocą standardowej biblioteki jest kilka opcji, ale nie są najwygodniejsze i raczej ograniczone. Można używać LinearLayout z wagami, RelativeLayout ze sztucznymi widokami, stworzyć własny layout albo z Javy mierzyć wysokość/szerokość ekranu i dopasować odpowiednio swój widok na podstawie specyficznych kryteriów.
Najlepiej jednak zaimportować z repozytorium Google ConstraintLayout i go użyć. https://developer.android.com/training/constraint-layout/index.html
- Rejestracja: dni
- Ostatnio: dni
wrap_content powoduje ze layout obrazka ma taki rozmiar jak obrazek
match_parent powoduje ze layout obrazka ma taki rozmiar jak layout w ktorym jest umieszczony, czyli wypelnia parent'a
masz jeszcze scaleType do ustawiania skalowania obrazka.
mozesz zadac dpi i one beda automatyczie dopasowywac sie do rozmiaru.
jak wpiszesz px to bedzie lipa.
wklej swoj kod i napisz co chcesz osiagnac dokladnie
- Rejestracja: dni
- Ostatnio: dni
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.app.enjoy.PlacesTab">
<TextView
android:id="@+id/dupa1Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="DUPA"
android:gravity="center"
android:layout_weight="1" />
<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_below="@+id/placeText"
android:layout_marginTop="20px">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="50dp"
android:minHeight="50dp"
android:maxHeight="5000dp"
android:maxWidth="5000dp"
android:layout_weight="0.3"
android:gravity="left"
android:src="@color/cardview_shadow_end_color" />
<TextView
android:id="@+id/dupa2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DUPA"
android:layout_weight="0.7" />
</LinearLayout>
</RelativeLayout>
I chce aby ImageView nie był większy niż jakaś 1/3 linii w której się znajduje wraz z textview ale dopasowywał sie do wielkości ekranu (1/3 na szerokość oraz wysokość jakaś wartość)
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Kraków
- Postów: 834
Możesz zrobić coś takiego, jeżeli dobrze Cię zrozumiałem. Musisz tylko dupta2Text odpowiednio umieścić w wewnątrz LinearLayout i ustawić wysokość swojego imageView na taką, jaką chcesz.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/dupa1Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="DUPA" />
<LinearLayout
android:weightSum="1"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/dupa1Text"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.33"
android:gravity="start"
android:src="@color/cardview_shadow_end_color" />
<TextView
android:layout_weight="0.66"
android:id="@+id/dupa2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DUPA" />
</LinearLayout>
</RelativeLayout>
Jednak lepiej jest zaprzyjaźnić się z ConstraintLayout. Po pierwsze będzie szybsze w tym wypadku. Po drugie, w przyszłości jak będziesz musiał tworzyć bardziej skomplikowane widoki, to znajomość ConstraintLayout zwróci się. To, o co musisz zadbać w tym przypadku, to jakie chcesz relatywne rozmiary szerokości do wysokości dla imageView - w tym przykładzie 16:9 (chyba, że wolisz np wrap_content).
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<TextView
android:id="@+id/dupa1Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="DUPA"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.33" />
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="start"
android:src="@color/cardview_shadow_end_color"
app:layout_constraintDimensionRatio="16:9"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/dupa1Text" />
<TextView
android:id="@+id/dupa2Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DUPA"
app:layout_constraintStart_toEndOf="@+id/guideline"
app:layout_constraintTop_toTopOf="@+id/imageView" />
</android.support.constraint.ConstraintLayout>