Android应用程序未按预期显示

时间:2017-09-28 20:02:51

标签: android xml display android-gridlayout

我使用android studio使用GridLayout创建一个简单的计算器应用程序。但是,它没有显示在我的设备上(是的,在我的旧note5而不是模拟器上运行它,因为它的速度要快得多)。在xml文件的设计选项卡中的android studio中,它显示了我的预期,但是当我在手机上运行应用程序时,事情有点奇怪。我将发布在手机上运行的应用程序的屏幕截图以及android studio design选项卡的屏幕截图。我还会在我的activity_main.xml中发布我的完整xml代码。

我的手机: enter image description here

Android Studio:

enter image description here

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.example.pvtboromeo.test.MainActivity"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        android:layout_row="0"
        android:layout_column="0"
        android:layout_columnSpan="4"
        android:layout_rowSpan="2"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:enabled="false"
        android:gravity="bottom|right"
        android:paddingRight="15sp"
        android:paddingBottom="15sp"
        android:textAlignment="gravity"
        android:textSize="40sp"
        android:text="TEST" />

    <Button
        android:id="@+id/buttonC"
        android:layout_column="0"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="C"/>

    <Button
        android:id="@+id/buttonNegative"
        android:layout_column="1"
        android:layout_row="2"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="+/-"/>

    <Button
        android:id="@+id/buttonPercent"
        android:layout_row="2"
        android:layout_column="2"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="%" />

    <Button
        android:id="@+id/buttonDivide"
        android:layout_row="2"
        android:layout_column="3"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:background="@drawable/button2"
        android:textSize="20sp"
        android:text="/" />

    <Button
        android:id="@+id/buttonSeven"
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="7"/>

    <Button
        android:id="@+id/buttonEight"
        android:layout_column="1"
        android:layout_row="3"
        android:layout_columnWeight="1"
        android:layout_rowWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="8"/>

    <Button
        android:id="@+id/buttonNine"
        android:layout_row="3"
        android:layout_column="2"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:background="@drawable/button1"
        android:textSize="20sp"
        android:text="9" />

    <Button
        android:id="@+id/buttonMultiply"
        android:layout_row="3"
        android:layout_column="3"
        android:layout_rowWeight="1"
        android:layout_columnWeight="1"
        android:background="@drawable/button2"
        android:textSize="20sp"
        android:text="X" />

</GridLayout>

我做错了什么?

2 个答案:

答案 0 :(得分:0)

android:layout_rowWeight="1"
android:layout_columnWeight="1"

此属性仅用于API21及更高版本。你应该使用supportv7  而不是GridLayout 添加这个

 compile 'com.android.support:gridlayout-v7:26.0.0-alpha1'

在您的模块build.gradle中。 然后像这样改变你的xml。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.example.pvtboromeo.test.MainActivity"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/editText"
        android:background="@android:color/black"
        android:textColor="@android:color/white"
        app:layout_row="0"
        app:layout_column="0"
        app:layout_columnSpan="4"
        app:layout_rowSpan="2"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        android:enabled="false"
        android:gravity="bottom|right"
        android:paddingRight="15sp"
        android:paddingBottom="15sp"
        android:textAlignment="gravity"
        android:textSize="40sp"
        android:text="TEST" />

    <Button
        android:id="@+id/buttonC"
        app:layout_column="0"
        app:layout_row="2"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="C"/>

    <Button
        android:id="@+id/buttonNegative"
        app:layout_column="1"
        app:layout_row="2"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="+/-"/>

    <Button
        android:id="@+id/buttonPercent"
        app:layout_row="2"
        app:layout_column="2"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="%" />

    <Button
        android:id="@+id/buttonDivide"
        app:layout_row="2"
        app:layout_column="3"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="/" />

    <Button
        android:id="@+id/buttonSeven"
        app:layout_column="0"
        app:layout_row="3"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="7"/>

    <Button
        android:id="@+id/buttonEight"
        app:layout_column="1"
        app:layout_row="3"
        app:layout_columnWeight="1"
        app:layout_rowWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="8"/>

    <Button
        android:id="@+id/buttonNine"
        app:layout_row="3"
        app:layout_column="2"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="9" />

    <Button
        android:id="@+id/buttonMultiply"
        app:layout_row="3"
        app:layout_column="3"
        app:layout_rowWeight="1"
        app:layout_columnWeight="1"
        android:background="@color/colorPrimary"
        android:textSize="20sp"
        android:text="X" />

</android.support.v7.widget.GridLayout>   

注意,这是android.support.v7.widget.GridLayout而不是android.widget.GridLayout,属性是。{ app:layout_rowWeight="1"不是android:layout_rowWeight="1"

答案 1 :(得分:0)

我建议你使用ConstraintLayout。是有更多视图的布局的更好方法。

这也是获得良好表现的最佳方式(而且您需要更少的时间进行编程)。

你的观点越多,你的活动就越慢。

对于计算器的简单布局,您可以尝试查看我的代码:

<?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.moris.calcolatricelayoutconstraint.MainActivity">

<TextView
    android:id="@+id/textView24"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="/"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView5"
    app:layout_constraintRight_toLeftOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/textView21" />

<TextView
    android:id="@+id/textView25"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="x"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView3"
    app:layout_constraintTop_toBottomOf="@+id/textView22" />

<TextView
    android:id="@+id/textView26"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="="
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView23" />

<TextView
    android:id="@+id/textView21"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="-"
    app:layout_constraintBottom_toTopOf="@+id/textView24"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView5"
    app:layout_constraintRight_toLeftOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/textView18" />

<TextView
    android:id="@+id/textView22"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="+"
    app:layout_constraintBottom_toTopOf="@+id/textView25"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView3"
    app:layout_constraintTop_toBottomOf="@+id/textView19" />

<TextView
    android:id="@+id/textView23"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="%"
    app:layout_constraintBottom_toTopOf="@+id/textView26"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView20" />

<TextView
    android:id="@+id/textView18"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="8"
    app:layout_constraintBottom_toTopOf="@+id/textView21"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView5"
    app:layout_constraintRight_toLeftOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/textView15" />

<TextView
    android:id="@+id/textView19"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="7"
    app:layout_constraintBottom_toTopOf="@+id/textView22"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView3"
    app:layout_constraintTop_toBottomOf="@+id/textView16" />

<TextView
    android:id="@+id/textView20"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="9"
    app:layout_constraintBottom_toTopOf="@+id/textView23"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView17" />

<TextView
    android:id="@+id/textView15"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="5"
    app:layout_constraintBottom_toTopOf="@+id/textView18"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView5"
    app:layout_constraintRight_toLeftOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/textView3" />

<TextView
    android:id="@+id/textView16"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="4"
    app:layout_constraintBottom_toTopOf="@+id/textView19"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView3"
    app:layout_constraintTop_toBottomOf="@+id/textView5" />

<TextView
    android:id="@+id/textView17"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="6"
    app:layout_constraintBottom_toTopOf="@+id/textView20"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toRightOf="@+id/textView3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView6" />

<TextView
    android:id="@+id/textView"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    app:layout_constraintBottom_toTopOf="@+id/textView3"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

<EditText
    android:id="@+id/editText"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginEnd="6dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="6dp"
    android:layout_marginStart="8dp"
    android:ems="10"
    android:hint="Insert numbers"
    android:inputType="textPersonName"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.0" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="2"
    app:layout_constraintBottom_toTopOf="@+id/textView15"
    app:layout_constraintLeft_toRightOf="@+id/textView5"
    app:layout_constraintRight_toLeftOf="@+id/textView6"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="1"
    app:layout_constraintBottom_toTopOf="@+id/textView16"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/textView3"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:background="@drawable/border"
    android:gravity="center"
    android:text="3"
    app:layout_constraintBottom_toTopOf="@+id/textView17"
    app:layout_constraintLeft_toRightOf="@+id/textView3"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView" />

要执行边框,我已在可绘制资源中创建此xml文件调用&#34; border&#34;,并在TextView的背景中使用它:

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android">

<stroke
    android:color="@color/colorPrimaryDark"
    android:width="2dp"/>

<corners
    android:radius="5dp"/>

如果您想了解有关ConstraintLayout的更多信息:https://developer.android.com/training/constraint-layout/index.html

特别注意视频4。