Как отобразить ImageView поверх Imageview - PullRequest
0 голосов
/ 27 апреля 2018

Мне нужно отобразить 4 изображения, как показано ниже на изображении. ширина каждого изображения должна составлять 2/5 ширины экрана, как указано на изображении ниже. Я пытался использовать LinearLayout, но не получил вывод, как я ожидал.

Output should be like this

Мой код xml,

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/card1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

    </LinearLayout>

Что я должен изменить?

Ответы [ 5 ]

0 голосов
/ 27 апреля 2018
public static int getScreenWidth(Context context) {

        DisplayMetrics displaymetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay()
                .getMetrics(displaymetrics);

        int height = displaymetrics.heightPixels;
        int width = displaymetrics.widthPixels;

        return width;
    }

используйте вышеупомянутый метод, чтобы вычислить ширину экрана после этого, умножьте свое отношение на ширину экрана:

int imageViewWidth = getScreenWidth(this) * (2/5);

теперь установите эту ширину для вашего imageView:

imageView.getLayoutParams().width = imageViewWidth;
 imageView.requestLayout();
0 голосов
/ 27 апреля 2018
    If you want to set an image over the image then you have to use `FrameLayout` or `RelativeLayout` instead of `LinearLayout`.

Otherwise, you have to use the weightSum="5"  in your Parent `LinearLayout`
<LinearLayout 
    android:weightSum="5">

    <ImageView
        android:layout_weight="1" />

    <ImageView
        android:layout_weight="1"/>

    <ImageView
        android:layout_weight="1"/>

    <ImageView
        android:layout_weight="2"/>

</LinearLayout>
0 голосов
/ 27 апреля 2018

Надеюсь, это сработает для вас

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
android:padding="10dp"
android:orientation="horizontal">

<ImageView
    android:id="@+id/card1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    android:src="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/card2"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    android:src="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/card3"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_weight="1"
    android:scaleType="fitCenter"
    android:src="@mipmap/ic_launcher" />

<ImageView
    android:id="@+id/card4"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginRight="2dp"
    android:layout_weight="2"
    android:scaleType="fitCenter"
    android:src="@mipmap/ic_launcher" />

</LinearLayout>
0 голосов
/ 27 апреля 2018

Вы устанавливаете layout_weight , как показано ниже:

<LinearLayout 
    android:weightSum="5">

    <ImageView
        android:layout_weight="1" />

    <ImageView
        android:layout_weight="1"/>

    <ImageView
        android:layout_weight="1"/>

    <ImageView
        android:layout_weight="2"/>

</LinearLayout>

[Примечание: установить вес, как указано выше, дальнейший код должен быть таким же]

0 голосов
/ 27 апреля 2018

Вам нужно использовать weightsum означает, что вам нужно дать три imageview как weight="1" и назначить weight="2" для четвертого вида изображения.

Затем, наконец, вам нужно добавить общий вес в weightsum="5" в родительском макете. Итак, вы можете получить 2/5 ширины экрана.

Пожалуйста, попробуйте это и замените ваш код ниже: -

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:weightSum="5"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/card1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="1"
            android:scaleType="fitCenter" />

        <ImageView
            android:id="@+id/card4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:layout_weight="2"
            android:scaleType="fitCenter" />

    </LinearLayout>

Это поможет вам.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...