Android 如何为钢琴创作版式

Android 如何为钢琴创作版式,android,layout,Android,Layout,我正在开发钢琴应用程序,我在布局创作方面遇到了一些问题。我想创建如下布局: 但我只能创造这个 现在我想添加所有的黑色按钮,但问题是我不能在这些按钮上面添加视图。我该怎么做?我目前的布局代码如下,请建议我如何实现这一点。现在我可以点击所有的按钮,一旦我添加了所有的按钮,那么每个按钮都应该是可以点击的 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.andr

我正在开发钢琴应用程序,我在布局创作方面遇到了一些问题。我想创建如下布局:

但我只能创造这个

现在我想添加所有的黑色按钮,但问题是我不能在这些按钮上面添加视图。我该怎么做?我目前的布局代码如下,请建议我如何实现这一点。现在我可以点击所有的按钮,一旦我添加了所有的按钮,那么每个按钮都应该是可以点击的

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:weightSum="1.5" >

        <ImageView
            android:id="@+id/bw1"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw2"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw3"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw4"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw5"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw6"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw7"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw8"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw9"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw10"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw11"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw12"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw13"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw14"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />

        <ImageView
            android:id="@+id/bw15"
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight=".1"
            android:background="@drawable/blacknew" />
    </LinearLayout>

</RelativeLayout> 


您可以通过使用布局容器获得所需的内容,使用布局容器可以覆盖按钮和标签

我建议您自己绘制这些黑白键。使用View类,重写draw()方法并执行它。当然,您可以尝试使用RelativeLayout,但稍后您会遇到更多问题(如多次触摸和幻灯片检测)

类扩展视图{
公共钢琴(背景){
超级(上下文);
//TODO自动生成的构造函数存根
}
位图白键、黑键;
油漆=新油漆();
公共空白绘制(画布){
if(whiteKey==null){
whiteKey=BitmapFactory.decodeResource(getResources(),R.drawable.whiteKey);
}
if(blackKey==null){
blackKey=BitmapFactory.decodeResource(getResources(),R.drawable.blackKey);
}
int键=10;
//画白键
对于(int i=0;i
好了。这很简单。您应该添加一些代码来确定键的数量。我不知道你是想要10、12、50还是尽可能多的键。谢谢你的指导。现在我可以用黑色键和白色键组合创建所需的视图。我正在创作15个白键和10个黑键的钢琴,我对画布是全新的,所以请你告诉我如何在每个键上弹奏不同的声音。我想你可以接受答案并添加另一个问题。这很简单,比如你需要把笔记录成mp3。然后,当有人按键时,你必须检测。要做到这一点,只需检查按键的矩形形状是否包含用户手指下的点。如果可能,请共享一些代码片段。我有所有的声音字节,现在我想,每当我按下任何键,它应该突出显示,并发挥相应的声音你的答案是可怕的,但你可以请分享一个代码片段以及。
class Piano extends View {
    public Piano(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    Bitmap whiteKey, blackKey;
    Paint paint = new Paint();

    public void draw(Canvas canvas) {
        if (whiteKey == null) {
            whiteKey = BitmapFactory.decodeResource(getResources(), R.drawable.whiteKey);
        }
        if (blackKey == null) {
            blackKey = BitmapFactory.decodeResource(getResources(), R.drawable.blackKey);
        }

        int keys = 10;

        // draw white keys
        for (int i = 0; i < keys; i++) {
            canvas.drawBitmap(whiteKey, i * whiteKey.getWidth(), 0, paint);
        }
        // draw black keys
        for (int i = 0; i < keys; i++) {
            if (i != 3 && i != 7) {
                canvas.drawBitmap(blackKey, i * blackKey.getWidth()+blackKey.getWidth()*0.5f, 0, paint);
            }
        }
    }
};