如何根据不同的屏幕尺寸设置按钮

时间:2018-10-18 19:19:22

标签: java android xml button layout

伙计们,请检查我的代码,我无法设置具有不同屏幕尺寸的按钮,我在xml中设置按钮,但是每当我在5.0屏幕尺寸上启动应用程序时,它的效果都很好,但是每当我在6.0或更高版本中打开我的应用程序时,然后在底部显示额外的空间,我尝试了所有可能的解决方案,但仍然没有解决方案。因此,请检查我的代码并查看屏幕截图,并为此提供正确的解决方案。

enter image description here

enter image description here

<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"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="5dp"
            android:layout_height="150dp"
            android:layout_marginLeft="10dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1"
            app:srcCompat="@drawable/cool" />


        <ImageButton
            android:id="@+id/imageButton1"
            android:layout_width="5dp"
            android:layout_marginLeft="10dp"
            android:background="@color/colorPrimary"
            android:layout_height="150dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/attitude" />

</LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_marginTop="170dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton2"
            android:layout_width="5dp"
            android:layout_marginLeft="10dp"
            android:background="@color/colorPrimary"
            android:layout_height="150dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/love" />


        <ImageButton
            android:id="@+id/imageButton3"
            android:layout_width="5dp"
            android:layout_height="150dp"
            android:layout_marginLeft="10dp"
            android:background="@color/colorPrimary"
            android:layout_weight="1"
            app:srcCompat="@drawable/broken" />


    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:orientation="horizontal"
        android:layout_marginTop="330dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <ImageButton
            android:id="@+id/imageButton4"
            android:layout_width="5dp"
            android:background="@color/colorPrimary"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            app:srcCompat="@drawable/funnn"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp" />


        <ImageButton
            android:id="@+id/imageButton5"
            android:layout_width="5dp"
            android:background="@color/colorPrimary"
            android:layout_height="150dp"
            android:layout_weight="1"
            android:layout_marginLeft="10dp"
            app:srcCompat="@drawable/sad"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="0dp" />

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

您应该删除LinearLayouts,并且应该为每个视图创建链。您将拥有三个不同的水平链(每行一个):

  • 父母<-> imageButton <-> imageButton1 <->父母
  • 父母<-> imageButton2 <-> imageButton3 <->父母
  • 父母<-> imageButton4 <-> imageButton5 <->父母

您将拥有两个不同的垂直链(每列一个):

  • 父母<-> imageButton <-> imageButton2 <-> imageButton4 <->父母
  • 父母<-> imageButton1 <-> imageButton3 <-> imageButton5 <->父母

这是xml:

<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:padding="2dp">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/imageButton2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageButton1"
        app:srcCompat="@drawable/cool"/>

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/imageButton3"
        app:layout_constraintStart_toEndOf="@+id/imageButton"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/attitude"/>

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@+id/imageButton"
        app:layout_constraintBottom_toTopOf="@+id/imageButton4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageButton3"
        app:srcCompat="@drawable/love"/>

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@+id/imageButton1"
        app:layout_constraintBottom_toTopOf="@+id/imageButton5"
        app:layout_constraintStart_toEndOf="@+id/imageButton2"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/broken"/>

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@+id/imageButton2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/imageButton5"
        app:srcCompat="@drawable/funnn"/>

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_margin="2dp"
        android:background="@color/colorPrimary"
        app:layout_constraintTop_toBottomOf="@+id/imageButton3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageButton4"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/sad"/>

</android.support.constraint.ConstraintLayout>