Android按钮约束和屏幕尺寸

时间:2017-07-10 07:39:43

标签: android

 <?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="com.example.hp.buttonconstraints.MainActivity">


   <Button
    android:id="@+id/answerBtnTwo"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="10dp"
    android:layout_marginLeft="6dp"
    android:layout_marginRight="10dp"
    android:layout_marginStart="6dp"
    android:background="@drawable/red_rectangle"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/answerBtnOne"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toRightOf="@+id/answerBtnOne"
    app:layout_constraintRight_toRightOf="@+id/questionBacksplash"
    app:layout_constraintTop_toTopOf="@+id/answerBtnOne"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1" />

   <Button
    android:id="@+id/answerBtnOne"
    android:layout_width="148dp"
    android:layout_height="56dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="10dp"
    android:layout_marginStart="10dp"
    android:background="@drawable/red_rectangle"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toTopOf="@+id/answerBtnThree"
    app:layout_constraintLeft_toLeftOf="@+id/questionBacksplash"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1" />

    <Button
    android:id="@+id/answerBtnThree"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:layout_marginBottom="64dp"
    android:background="@drawable/red_rectangle"
    android:elevation="1dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toTopOf="@+id/answerBtnSeven"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintLeft_toLeftOf="@+id/answerBtnOne"
    app:layout_constraintRight_toRightOf="@+id/answerBtnOne"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:targetApi="lollipop" />

   <Button
    android:id="@+id/answerBtnFive"
    android:layout_width="148dp"
    android:layout_height="56dp"
    android:layout_marginBottom="60dp"
    android:layout_marginEnd="1dp"
    android:layout_marginRight="1dp"
    android:layout_marginTop="60dp"
    android:background="@drawable/red_rectangle"
    android:elevation="1dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/answerBtnSeven"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintRight_toRightOf="@+id/answerBtnThree"
    app:layout_constraintTop_toTopOf="@+id/answerBtnThree"
    app:layout_constraintVertical_bias="0.0"
    tools:ignore="HardcodedText,UnusedAttribute"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1" />

    <Button
    android:id="@+id/answerBtnSeven"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/red_rectangle"
    android:elevation="1dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="@+id/answerBtnEight"
    app:layout_constraintLeft_toLeftOf="@+id/answerBtnThree"
    app:layout_constraintRight_toRightOf="@+id/answerBtnThree"
    app:layout_constraintTop_toTopOf="@+id/answerBtnEight"
    tools:ignore="HardcodedText,UnusedAttribute"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1"
    tools:layout_constraintTop_creator="1" />

     <Button
    android:id="@+id/answerBtnEight"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:layout_marginBottom="29dp"
    android:background="@drawable/red_rectangle"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="@+id/answerBtnSix"
    app:layout_constraintRight_toRightOf="@+id/answerBtnSix"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

  <Button
    android:id="@+id/answerBtnSix"
    android:layout_width="0dp"
    android:layout_height="56dp"
    android:layout_marginBottom="4dp"
    android:background="@drawable/red_rectangle"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toTopOf="@+id/answerBtnEight"
    app:layout_constraintLeft_toLeftOf="@+id/answerBtnFour"
    app:layout_constraintRight_toRightOf="@+id/answerBtnFour"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

  <Button
    android:id="@+id/answerBtnFour"
    android:layout_width="148dp"
    android:layout_height="56dp"
    android:layout_marginBottom="4dp"
    android:layout_marginLeft="1dp"
    android:layout_marginStart="1dp"
    android:background="@drawable/red_rectangle"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:text="Text"
    android:textColor="@android:color/black"
    android:textSize="12sp"
    app:layout_constraintBottom_toTopOf="@+id/answerBtnSix"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="@+id/answerBtnTwo"
    tools:ignore="HardcodedText"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1" />

  <ImageButton
    android:id="@+id/questionBacksplash"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="9dp"
    android:layout_marginEnd="19dp"
    android:layout_marginLeft="19dp"
    android:layout_marginRight="19dp"
    android:layout_marginStart="19dp"
    android:background="@null"
    app:layout_constraintBottom_toTopOf="@+id/answerBtnOne"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
   tools:ignore="ContentDescription"
    tools:layout_constraintBottom_creator="1"
    tools:layout_constraintLeft_creator="1"
    tools:layout_constraintRight_creator="1" />

............................................... ...

我正在尝试在约束模式下向页面添加一些按钮,但我需要它们在不同的屏幕上调整大小。按钮1显示宽度为148dp,适用于Galaxy Nexus但不适用于Nexus 4.Nexus 4需要为158dp。但是,当我在Nexus 4布局中调整按钮大小时,它会在Galaxy布局中缩小。有人可以建议我如何绕过这个

1 个答案:

答案 0 :(得分:1)

为什么你不给你的按钮android:layout_width android:layout_height !!从dimens给出它们的大小,并使用不同的dimens从资源到不同的屏幕尺寸