如何为钢琴创建布局 [英] How to create layout for piano

查看:39
本文介绍了如何为钢琴创建布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working on piano app and I am facing some problems with the layout creation. I want to create a layout like below:

但我只能创建这个

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

Now I want to add all the black buttons but problem is that I am not able to add view above those buttons. How can I do this? My current layout code is below, please suggest me how can I achieve this. Now I am able to click all the buttons, once I add all the buttons then each button should be clickable.

<?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,但之后你会遇到更多的问题(比如多次触摸和滑动检测).

I would suggest drawing these white and black keys on your own. Use the View class, override draw() method and go for it. Of course you can try to use RelativeLayout, but later you'll have more problems with that (like multiple touches and slides detection).

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);
            }
        }
    }
};

这篇关于如何为钢琴创建布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆