1

嗨,我已经设置(很好地膨胀)了一堆按钮,这些按钮是从包含按钮、文本视图等的相对布局构建的。我希望能够删除/隐藏/删除整个按钮集。我假设我可以setVisibility(View.GONE)在 RelativeLayout 上做一个,但我不确定我应该如何引用这个布局。我已经为它设置了一个 ID,但是在尝试引用它时我真的迷路了。启动此代码的按钮是删除按钮,因此 OnClick(View v) 包含删除按钮的 ID。我刚刚尝试了一个 getparent,但效果不佳(崩溃)。

谁能指出我正确的方向?

这是我尝试过的getparent代码。

            RelativeLayout dropping = (RelativeLayout) ((Activity) v.getParent()).findViewById(R.id.tp_container);
            dropping.setVisibility(View.GONE);

我希望有这样的东西,虽然 id 可以用另一个变量表示,如果可能的话?

relativeViewVariable.id(actualId).setVisibility(View.GONE);

这很尴尬,但我不知道这是Java还是Android问题?

干杯

4

5 回答 5

2

如果您动态创建视图,则必须设置自定义 ID,然后从其父级获取具有该 ID 的视图!.

View textView;
RelativeLayout layout = new RelativeLayout (this);
setContentView(layout);

for(int i=0;i<10;i++){
        textView = new TextView(this);
        textView.setId(i);
        layout.addView(textView);
}

/* FOR LOOP THAT ONE */
TextView referencedTextView = (TextView) layout.findViewById(5);
referencedTextView.setVisibility(View.GONE);

如果您只想隐藏布局中的所有按钮,则可以遍历所有父视图。

LinearLayout layout;
        View view;
        for (int i = 0; i < layout.getChildCount(); i++) {
            if(layout.getChildAt(i) instanceof Button){
                ((Button)layout.getChildAt(i)).setVisibility(View.GONE);
            }

        }
于 2012-04-04T11:02:54.433 回答
0

如果您在 RelativeLayout 中有 childViews 的 id,则很有可能做到这一点。

dropping.findViewByID(R.id.ids).setVisibility(View.GONE)
dropping.removeView(dropping.findViewByID(R.id.ids))

等等..

于 2012-04-04T10:53:56.607 回答
0

使用 ViewGroup 创建一组视图。这里 ViewGroup 将是基类。将 ViewGroup 的可见性设置为 GONE 将导致将其设置为子视图

于 2012-04-04T10:55:02.567 回答
0
View menu; //Declaration
View app; //Declaration

身体的一部分

final View[] children = new View[] { menu, app };

    public void initViews(View[] children)
    {
        // A ViewGroup MUST be the only child of the HSV
        ViewGroup parent = (ViewGroup) getChildAt(0);

        if(Your Condition)
        {
        children[i].setVisibility(View.INVISIBLE);
        parent.addView(children[i]);
        }       
    }

我已经在我的应用程序中完成了这种工作,只需在上述功能中创建自己的条件并让它工作。

于 2012-04-04T11:02:50.790 回答
0

Of course it an android Question

first you have inflated layout so the its instant create only once .

suppose u have created five layout using looping inflated layout then its all ID remain same (that's true the view create multiple times ) so you cant justify the its using ID .

if you want to so what u describe in your question then you should create dynemically layout

using in the java file . like

for(int i = 0 ; i < 10  ;  i ++)
{
  RelativeLayout layout = new RelativeLayout(context);  
}

after that it will create new layout ID for each layout or layout component .

于 2012-04-04T11:03:57.683 回答