Как сделать так, чтобы высота кнопки менялась автоматически в зависимости от экрана? - PullRequest
0 голосов
/ 01 декабря 2018

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

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".Main3Activity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="144dp"
        android:layout_height="133dp"
        app:srcCompat="@android:drawable/btn_star_big_on"
        tools:layout_editor_absoluteX="133dp"
        tools:layout_editor_absoluteY="40dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/imageView"
        android:text="Button1"
        tools:layout_editor_absoluteX="36dp"
        tools:layout_editor_absoluteY="184dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button1"
        android:text="Button2"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="260dp" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/button2"
        android:text="Button3"
        tools:layout_editor_absoluteX="161dp"
        tools:layout_editor_absoluteY="340dp" />



</android.support.constraint.ConstraintLayout>

Вот как это выглядит в 6"экран и в 5": enter image description here

enter image description here

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

enter image description here

enter image description here

Спасибо за вашу помощь.(Я думаю, что использование match_parent в layout_height будет решением, но я пробовал и не знаю, как его использовать)

Ответы [ 2 ]

0 голосов
/ 01 декабря 2018

Если вы хотите использовать LinearLayout и установить одинаковый вес для всех 3 кнопок:

<?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"
    android:orientation="vertical"
    tools:context=".Main3Activity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="144dp"
        android:layout_height="133dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@android:drawable/btn_star_big_on" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button1"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button2"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginLeft="70dp"
        android:layout_marginRight="70dp"
        android:layout_marginTop="15dp"
        android:text="Button3"
        android:layout_weight="1" />
</LinearLayout>
0 голосов
/ 01 декабря 2018

Вы можете использовать LinearLayout или TableLayout и сделать высоту всех кнопок match_parent , затем дать всем кнопкам weight свойство

android:layout_weight="1"
...