[英]How to create layout for piano
我正在研究钢琴应用程序,我在布局创建方面遇到了一些问题。 我想创建一个如下所示的布局:
但我只能创造这个
现在我想添加所有黑色按钮,但问题是我无法在这些按钮上方添加视图。 我怎样才能做到这一点? 我目前的布局代码如下,请建议我如何实现这一目标。 现在我可以点击所有按钮,一旦我添加了所有按钮,那么每个按钮都应该是可点击的。
<?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,但是稍后你会遇到更多问题(比如多次触摸和幻灯片检测)。
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);
}
}
}
};
你可以通过使用布局容器得到你想要的东西,这样可以叠加按钮和标签
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.