我有兩個ImageButtons,每個裏面的一個RelativeLayout和這兩個RelativeLayouts是在另一個RelativeLayout中,我想爲每個ImageButton設置TouchDelegate。如果通常我將TouchDelegate添加到每個ImageButton並且它的父親是RelativeLayout,那麼只有一個ImageButton可以正常工作,另一個不會擴展它的點擊區域。因此,PLease幫助我瞭解如何在ImageButton中使用TouchDelegate。如果這是不可能的,那麼擴展視圖點擊區域的有效方法是什麼?在此先感謝........如何使用多個TouchDelegate
這裏是我的XML代碼:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/FrameContainer"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout3" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout1" android:layout_width="113dip"
android:layout_height="25dip">
<ImageButton android:id="@+id/tutorial1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:src="@drawable/tutorial" />
</RelativeLayout>
<RelativeLayout android:layout_alignParentLeft="true"
android:id="@+id/relativeLayout2" android:layout_width="113dip"
android:layout_height="25dip" android:layout_marginLeft="100dip">
<ImageButton android:id="@+id/tutorial2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@null" android:src="@drawable/tutorial"
android:layout_marginLeft="50dip" />
</RelativeLayout>
</RelativeLayout>
我的動態類:
import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.TouchDelegate;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class TestTouchDelegate extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View mParent1 = findViewById(R.id.relativeLayout1);
mParent1.post(new Runnable() {
@Override
public void run() {
Rect bounds1 = new Rect();
ImageButton mTutorialButton1 = (ImageButton) findViewById(R.id.tutorial1);
mTutorialButton1.setEnabled(true);
mTutorialButton1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 1", Toast.LENGTH_SHORT).show();
}
});
mTutorialButton1.getHitRect(bounds1);
bounds1.right += 50;
TouchDelegate touchDelegate1 = new TouchDelegate(bounds1, mTutorialButton1);
if (View.class.isInstance(mTutorialButton1.getParent())) {
((View) mTutorialButton1.getParent()).setTouchDelegate(touchDelegate1);
}
}
});
//View mParent = findViewById(R.id.FrameContainer);
View mParent2 = findViewById(R.id.relativeLayout2);
mParent2.post(new Runnable() {
@Override
public void run() {
Rect bounds2 = new Rect();
ImageButton mTutorialButton2 = (ImageButton) findViewById(R.id.tutorial2);
mTutorialButton2.setEnabled(true);
mTutorialButton2.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(TestTouchDelegate.this, "Test TouchDelegate 2", Toast.LENGTH_SHORT).show();
}
});
mTutorialButton2.getHitRect(bounds2);
bounds2.left += 50;
TouchDelegate touchDelegate2 = new TouchDelegate(bounds2, mTutorialButton2);
if (View.class.isInstance(mTutorialButton2.getParent())) {
((View) mTutorialButton2.getParent()).setTouchDelegate(touchDelegate2);
}
}
});
}
}
我試圖重寫getHitRect,但它似乎並沒有工作(它根本沒有被調用)。基本上我創建了一個自定義視圖,它擴展了ImageButton,然後在類中重寫getHitRect(帶有添加的填充),但正如我所提到的,getHitRect永遠不會被調用。是否可以發佈您的解決方案? – stephenteh
事實證明,我提供的解決方案在ICS中不起作用,這可能是它爲什麼不適合你的原因。我寫了一篇關於兩種不同觸摸委託策略的博客文章http://www.brendanweinstein.me/2012/06/26/touchdelegate-and-gethitrect-making-buttons-with-expanded-touch-areas/,您可以獲取樣本https://github.com/brendanw/Touch-Delegates/blob/master/src/com/brendan/touchdelegates/MyTouchDelegate.java上的兩種策略代碼 –
這不回答OP的問題。 OP不會嘗試將多個代理添加到一個視圖。 OPs例子中的兩個ImageButton具有*不同的父母。 – zyamys