小部件不会按照我在android studio中的排列方式出现

时间:2018-09-16 09:39:11

标签: android

当我尝试制作计算器程序时,运行模拟器the appearance of widgets时,按钮的外观类似于当我重新排列按钮7时,小部件消失的情况the widgets disappeared the design

activity_main.xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <Button
        android:id="@+id/button17"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button14"
        android:layout_alignTop="@+id/button11"
        android:text="*" />

    <Button
        android:id="@+id/button16"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/button3"
        android:layout_marginEnd="48dp"
        android:text="/" />

    <Button
        android:id="@+id/button15"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button16"
        android:layout_alignTop="@+id/button5"
        android:text="-" />

    <Button
        android:id="@+id/button14"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button15"
        android:layout_alignTop="@+id/button"
        android:text="+" />

    <Button
        android:id="@+id/button13"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button3"
        android:layout_alignTop="@+id/button11"
        android:text="7" />

    <Button
        android:id="@+id/button12"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_alignTop="@+id/button13"
        android:text="9" />

    <Button
        android:id="@+id/button11"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button4"
        android:layout_alignTop="@+id/button12"
        android:text="8" />

    <Button
        android:id="@+id/button9"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_alignTop="@+id/button3"
        android:text="6" />

    <Button
        android:id="@+id/button10"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button4"
        android:layout_alignTop="@+id/button3"
        android:text="5" />

    <Button
        android:id="@+id/button5"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/button2"
        android:layout_alignTop="@+id/button6"
        android:text="3" />

    <Button
        android:id="@+id/button6"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/button4"
        android:layout_marginBottom="136dp"
        android:text="2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignTop="@+id/button"
        android:layout_marginEnd="130dp"
        android:text="." />

    <Button
        android:id="@+id/button3"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/button7"
        android:layout_marginBottom="204dp"
        android:text="4" />

    <Button
        android:id="@+id/button7"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/button5"
        android:layout_marginStart="42dp"
        android:text="1" />

    <Button
        android:id="@+id/button4"
        android:layout_width="47dp"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignTop="@+id/button"
        android:layout_marginStart="132dp"
        android:text="0" />

    <Button
        android:id="@+id/button"
        android:layout_width="73dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="60dp"
        android:layout_marginStart="26dp"
        android:text="clear" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="197dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="79dp"
        android:layout_marginTop="134dp"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="194dp"
        android:layout_height="wrap_content"
        android:layout_alignEnd="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_marginTop="70dp"
        android:text="TextView" />
</RelativeLayout>

MainActivity Java代码:

package com.example.amr.calc;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

3 个答案:

答案 0 :(得分:3)

您可能想尝试一下ConstraintLayout。最初的学习曲线可能会有些陡峭,但值得一读,可以用它构建几乎任何布局。

我不久前为自己的计算器应用构建了以下布局。

enter image description here

<?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">

<TextView
    android:padding="8dp"
    android:id="@+id/textViewInputOutput"
    android:layout_width="0dp"
    android:layout_height="150dp"
    android:ems="10"
    android:gravity="end"
    android:textSize="38sp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/textViewOne"
    style="@style/NumberText"
    android:layout_marginTop="8dp"
    android:text="1"
    app:layout_constraintBottom_toTopOf="@+id/textViewFour"
    app:layout_constraintEnd_toStartOf="@+id/textViewTwo"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewClear" />

<TextView
    android:id="@+id/textViewTwo"
    style="@style/NumberText"
    android:layout_marginTop="8dp"
    android:text="2"
    app:layout_constraintBottom_toTopOf="@+id/textViewFive"
    app:layout_constraintEnd_toStartOf="@+id/textViewThree"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewOne"
    app:layout_constraintTop_toBottomOf="@+id/textViewClear" />

<TextView
    android:id="@+id/textViewThree"
    style="@style/NumberText"
    android:layout_marginTop="8dp"
    android:text="3"
    app:layout_constraintBottom_toTopOf="@+id/textViewSix"
    app:layout_constraintEnd_toStartOf="@+id/textViewDelete"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewTwo"
    app:layout_constraintTop_toBottomOf="@+id/textViewClear" />

<TextView
    android:id="@+id/textViewDelete"
    style="@style/NumberText"
    android:layout_marginEnd="8dp"
    android:text="DEL"
    app:layout_constraintBottom_toBottomOf="@+id/textViewClear"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="@+id/textViewClear" />

<TextView
    android:textSize="@dimen/operator_text"
    android:id="@+id/textViewDivide"
    style="@style/NumberText"
    android:text="÷"
    app:layout_constraintBottom_toTopOf="@+id/textViewAdd"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/textViewDelete"
    app:layout_constraintTop_toBottomOf="@+id/textViewDelete" />

<TextView
    android:textSize="@dimen/operator_text"
    android:id="@+id/textViewAdd"
    style="@style/NumberText"
    android:text="+"
    app:layout_constraintBottom_toTopOf="@+id/textViewSubtract"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/textViewDelete"
    app:layout_constraintTop_toBottomOf="@+id/textViewDivide" />

<TextView
    android:textSize="@dimen/operator_text"
    android:id="@+id/textViewSubtract"
    style="@style/NumberText"
    android:text="−"
    app:layout_constraintBottom_toTopOf="@+id/textViewMultiply"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="@+id/textViewDelete"
    app:layout_constraintTop_toBottomOf="@+id/textViewAdd" />

<TextView
    android:textSize="@dimen/operator_text"
    android:id="@+id/textViewMultiply"
    style="@style/NumberText"
    android:text="×"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintStart_toStartOf="@+id/textViewSubtract"
    app:layout_constraintTop_toBottomOf="@+id/textViewSubtract" />

<TextView
    android:id="@+id/textViewFour"
    style="@style/NumberText"
    android:text="4"
    app:layout_constraintBottom_toTopOf="@+id/textViewSeven"
    app:layout_constraintEnd_toStartOf="@+id/textViewFive"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewOne" />

<TextView
    android:id="@+id/textViewFive"
    style="@style/NumberText"
    android:text="5"
    app:layout_constraintBottom_toTopOf="@+id/textViewEight"
    app:layout_constraintEnd_toStartOf="@+id/textViewSix"
    app:layout_constraintStart_toEndOf="@+id/textViewFour"
    app:layout_constraintTop_toBottomOf="@+id/textViewTwo" />

<TextView
    android:id="@+id/textViewSix"
    style="@style/NumberText"
    android:text="6"
    app:layout_constraintBottom_toTopOf="@+id/textViewNine"
    app:layout_constraintEnd_toStartOf="@+id/textViewDelete"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewFive"
    app:layout_constraintTop_toBottomOf="@+id/textViewThree" />

<TextView
    android:id="@+id/textViewSeven"
    style="@style/NumberText"
    android:text="7"
    app:layout_constraintBottom_toTopOf="@+id/textViewDecimal"
    app:layout_constraintEnd_toStartOf="@+id/textViewEight"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewFour" />

<TextView
    android:id="@+id/textViewEight"
    style="@style/NumberText"
    android:text="8"
    app:layout_constraintBottom_toTopOf="@+id/textViewZero"
    app:layout_constraintEnd_toStartOf="@+id/textViewNine"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewSeven"
    app:layout_constraintTop_toBottomOf="@+id/textViewFive" />

<TextView
    android:id="@+id/textViewNine"
    style="@style/NumberText"
    android:text="9"
    app:layout_constraintBottom_toTopOf="@+id/textViewEquals"
    app:layout_constraintEnd_toStartOf="@+id/textViewDelete"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewEight"
    app:layout_constraintTop_toBottomOf="@+id/textViewSix" />

<TextView
    android:id="@+id/textViewDecimal"
    style="@style/NumberText"
    android:text="."
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textViewZero"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewSeven" />

<TextView
    android:id="@+id/textViewZero"
    style="@style/NumberText"
    android:text="0"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textViewEquals"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewDecimal"
    app:layout_constraintTop_toBottomOf="@+id/textViewEight" />

<TextView
    android:id="@+id/textViewEquals"
    style="@style/NumberText"
    android:text="="
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toStartOf="@+id/textViewDelete"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewZero"
    app:layout_constraintTop_toBottomOf="@+id/textViewNine" />

<TextView
    android:id="@+id/textViewClear"
    style="@style/NumberText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:text="Clear"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewInputOutput" />

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="0dp"
    android:layout_height="1dp"
    android:background="@color/colorAccent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textViewInputOutput">

</FrameLayout>

<TextView
    android:id="@+id/textViewOpen"
    style="@style/NumberText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="("
    app:layout_constraintBottom_toBottomOf="@+id/textViewClear"
    app:layout_constraintEnd_toStartOf="@+id/textViewClose"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewClear"
    app:layout_constraintTop_toTopOf="@+id/textViewClear" />

<TextView
    android:id="@+id/textViewClose"
    style="@style/NumberText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text=")"
    app:layout_constraintBottom_toBottomOf="@+id/textViewOpen"
    app:layout_constraintEnd_toStartOf="@+id/textViewDelete"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/textViewOpen"
    app:layout_constraintTop_toTopOf="@+id/textViewOpen" />
</android.support.constraint.ConstraintLayout>

答案 1 :(得分:0)

您正在使用RelativeLayout,它相对于彼此排列视图,因此,当您重新排列button7时,相对于button7的所有其他小部件都会发生变化,依此类推。为此,您必须确保特定窗口小部件位置的任何更改都不会影响其他窗口小部件。除了这种方法(一种简单的方法)之外,您还可以对按钮使用LinearLayoutGridLayout以获得所需的格式。

答案 2 :(得分:0)

这种像您那样设计的xml不好,它更喜欢使用linearLayout或做得更好,但我发现了您的问题 问题是因为这条线

android:layout_alignTop="@+id/button11"

将按钮7更改为

 <Button
    android:id="@+id/button13"
    android:layout_width="47dp"
    android:layout_height="wrap_content"
    android:layout_alignStart="@+id/button3"
    android:layout_above="@+id/button3"
    android:text="7" />

这将解决您的问题

实际上您将按钮8放在按钮7的右侧,然后将按钮7放在按钮8的顶部,您的问题是这样的

我希望它能对您有所帮助(;