有没有一种方法可以使按下底部的按钮时,按钮不消失在另一个顶部?

问题描述 投票:0回答:1

我正在尝试将Midi键盘编程为移动应用程序,但是当我按下底部的一个按钮时,它们遇到的一些问题在于按钮键,它们会越过黑键(应该在键盘上最佳)我已经搜索了SO,但发现了“ Relativelayout”一词,但我没有成功使用它,实际上,当我在黑键上使用它时,它们只是消失了,我不能说为什么。

white on black key

这是我的第一个按钮/键的xml代码

 <Button
    android:id="@+id/buttonC"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:layout_marginEnd="16dp"
    android:backgroundTint="#FFFFFF"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

这是上面带有相对布局的黑键,当我使用它时它会消失

 <RelativeLayout

    android:id="@+id/button8"
    android:layout_width="0dp"
    android:layout_height="36dp"
    android:layout_marginStart="180dp"
    android:layout_marginTop="40dp"
    android:layout_marginEnd="16dp"
    android:background="#000000"
    android:backgroundTint="#000000"
    android:elevation="2pt"
    android:text="C#"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.51"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

这是普通黑键的代码

 <Button
    android:id="@+id/buttonB"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:backgroundTint="#FFFFFF"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/buttonA" />
java android xml button layout
1个回答
0
投票

您会错误地使用相对布局。相对布局是一个视图组。如果您不了解视图和视图组之间的区别,请在Google上了解它。

现在要解决问题,您应该将白色和黑色按钮都放在相对布局中。白色按钮将是第一个孩子,黑色按钮将是第二个孩子,您只需要告诉相对布局按钮的位置即可。因此,它将执行的操作是,首先绘制白色按钮,然后在其上方绘制黑色按钮。我正在尝试在下面添加示例代码,希望对您有所帮助:

<RelativeLayout 
    android:id="@+id/button_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
       android:id="@+id/white_button"
       android:layout_width="match_parent"
       android:layout_height="match_parent"/>
    <Button
       android:id="@+id/black_button"
       android:layout_width="30dp"
       android:layout_height="30dp"
       android:layout_alignParentRight="true"/>
</RelativeLayout> 

根据需要将其他属性添加到xml中。我给了黑色按钮30dp高度和宽度,请根据需要进行调整。而且我认为您理解为什么白色按钮的width和height属性设置为match_parent。让我知道,如果您需要更多帮助。编码愉快!

© www.soinside.com 2019 - 2024. All rights reserved.