1

我正在LinearLayout动态创建一个。我需要删除LinearLayoutonLongPress事件。

我的代码:

public void addTileView(View v) {         
      _parentLayout = (LinearLayout) findViewById(R.id.gridCont); 

    View child = getLayoutInflater().inflate(R.layout.customtileview,null);
     ((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);       
    _parentLayout.addView(child);       
    _parentLayout.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();

            return true;
        }
    });

}

这个怎么做 ?

4

3 回答 3

3

尝试这个,

_parentLayout.setOnLongClickListener(new OnLongClickListener() { 
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();

           parentLayout.removeAllViews();
        return true;
    }
});
于 2013-09-25T06:00:46.080 回答
2

最好删除您在 parent 中添加的视图。说父布局是主要布局,子布局是您要删除的布局。你应该试试 parent_layout.removeView(child_layout);

removeAllViews() - 将删除视图内的所有视图,但不删除主视图。

请参考 ViewGroup ViewGroup vg = (ViewGroup)(myView.getParent()); vg.removeView(myView);

或者,您可以将视图的可见性设置为 Visible.GONE ,然后在需要时使其可见。

于 2013-09-25T06:07:28.480 回答
0

这对我有用。

public void addTileView(View v) {

      _parentLayout = (LinearLayout) findViewById(R.id.gridCont);

      child = getLayoutInflater().inflate(R.layout.customtileview,null);
     ((TextView)child.findViewById(R.id.tileText)).setText("Tile View :"+_tileViewCount++);



    _parentLayout.addView(child);

    child.setOnLongClickListener(new OnLongClickListener() { 
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "Delete", Toast.LENGTH_SHORT).show();
            _parentLayout.removeView(v);
            return true;
        }
    });



}

谢谢

于 2013-09-25T06:26:49.330 回答