我有一个父布局,它是线性布局。在这个parentLayout中,我有一个子布局。使用这个子布局,我创建了一个按钮数组。我的代码如下所示:
XML布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/ParentLayout"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<ViewFlipper android:id="@+id/flipview" android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="15dp">
<LinearLayout android:id="@+id/liVLayout" android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ViewFlipper>
</LinearLayout>java代码:
public void onCreate(Bundle savedInstanceState)
{
//Setup the Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar_frm);
LinearLayout layMain = (LinearLayout) findViewById(R.id.parentLayout);
layMain.setOnTouchListener((OnTouchListener) this);
}用于子布局上按钮数组的方法:
public boolean initDay()
{
LinearLayout layoutVertical = (LinearLayout) findViewById(R.id.liVLayout);
LinearLayout rowLayout = null;
LayoutParams param = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1);
//Create Button
for (int i = 0; i<6; i++)
{
rowLayout = new LinearLayout(this);
rowLayout.setWeightSum(7);
layoutVertical.addView(rowLayout, param);
for(int j=0; j<7; j++)
{
m_pBtnDay[i][j] = new Button(this);
m_pBtnDay[i][j].setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
rowLayout.addView(m_pBtnDay[i][j], param);
// m_pBtnDay[i][j].setOnLongClickListener(this);
// m_pBtnDay[i][j].setOnClickListener(this);
m_pBtnDay[i][j].setOnTouchListener(this);
//save button position
m_pBtnDay[i][j].setTag(new CalendarForm(i , j));
}
}
return true;
}我的问题是,子布局包含一个包含父布局的按钮数组。
因此,我想为父布局调用的任何onListenerEvent都不适用于子布局所涵盖的部分。在我的例子中,我在parentLayout上调用了parentLayout。
因此,所发生的情况是,子布局所涵盖的部分对事件没有行为,但是子布局覆盖的ParentLayout对该事件没有行为,尽管它是ParentLayout的childlayout。
发布于 2011-09-08 13:26:17
好吧,它是正常工作的。例如,如果你把一张纸放在一张桌子的顶部,那么当你触摸到纸时,桌子将不知道它是否被触摸,因为纸张处理了触摸(忘记了触摸时的压力:)
您可以这样做的唯一方法是确保对于特定区域,子区域不应该处理事件- return false。由于子视图不处理事件,然后将其传递到作为父视图的底层视图。
这里有一些伪代码可以让您开始工作。
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if(v == child){
if((event.getX && event.getY) is within ParentArea){
//return false so that the parent will get the event.
return false;
}
else{
//handle child event
}
}
else if(v == parent){
//handle event
}
break;
}
return true;
}https://stackoverflow.com/questions/7332924
复制相似问题