[英]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.