Сделать изображение на половине экрана

У меня возникли проблемы с отображением моего изображения. Половина экрана может помочь кому-то. вот мой xml файл. Он должен занимать половину размера экрана, а также линейный макет с текстом должен находиться на нижнем колонтитуле изображения.

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>

Ответы

Ответ 1

Используйте LinearyLayout для переноса вашего ImageView и другого невидимого представления и установите layout_weight как на 0.5, тогда ImageView должен быть в два раза меньше.

Вот пример, показывающий, как установить изображение на половину размера экрана

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

И это результат

enter image description here

Если вы хотите поместить свое изображение в центр экрана, вы можете использовать ту же идею и установить правильный вес для достижения своей цели. Удачи!

Ответ 2

Сначала вам нужно получить размеры дисплея во время выполнения

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
windowWidth = size.x;
windowHeight = size.y;

Затем вы можете перейти к настройкам параметров высоты и ширины imageView до половины этих значений.

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

Ответ 3

Просто, если кто-то хочет, чтобы альбомная версия была почти такой же:)

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

Ответ 4

Вы должны поместить свое изображение в LinearLayout с горизонтальным выравниванием, чтобы вы могли использовать

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)