Как сделать так, чтобы высота imageButton занимала 25% размеров экрана во вложенном линейном макете. Android XML макет ландшафта - PullRequest
0 голосов
/ 05 октября 2019

enter image description here

Я хочу отобразить 9 изображений. Я хочу, чтобы макет был 3 к 3.

Проблема в том, что первые 3 изображения в первом ряду занимают две трети размера экрана.

Я попытался добавить вложенныйгоризонтальная линейная разметка внутри вертикальной линейной разметки с wightsum = 3, 1 для каждой горизонтальной линейной разметки. Я также добавил app: layout_heightPercent = "25%" app: layout_widthPercent = "25%" и ничего не изменилось.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    tools:context=".MainActivity"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="3"
    android:orientation="vertical">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="3"
        android:orientation="horizontal">


        <ImageButton
            android:id="@+id/image11"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:background="@drawable/image1"
            android:padding="10dp"
            android:scaleType="fitXY"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%"/>

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image2"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image3"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />
    </LinearLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="3">


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image4"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image5"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/ima6"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />
    </LinearLayout>
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:weightSum="3">


        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image7"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image8"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />

        <ImageButton
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:background="@drawable/image1"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:adjustViewBounds="true"
            android:padding="10dp"
            android:scaleType="fitXY"
            android:id="@+id/image9"
            android:layout_gravity="center"
            app:layout_heightPercent="25%"
            app:layout_widthPercent="25%" />
    </LinearLayout>

</LinearLayout>

1 Ответ

0 голосов
/ 09 октября 2019

Если вы просто хотите получить кнопку ширины и высоты, вы можете установить высоту и ширину кнопки макета с помощью этой функции

 private fun calculateImageSize() {
        val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val displayMetrics = DisplayMetrics()
        windowManager.defaultDisplay.getMetrics(displayMetrics)
        var width = displayMetrics.widthPixels
        //div 3 because want screen (3*3) if u want 25% you can change 4 (25+25+25+25)
        var realWitdh = width / 3 
        var mWidthMenu = realWitdh
        var mHeightMenu = realWidth
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...