4

I'm new to developing with android. I have a grid contained in a LinearLayout and each item which makes up the grid is a button. I want this LinearLayout to be invisible when the user pushes any of these buttons.

This is my 'home' layout shell:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
  <TextView/>
  <LinearLayout>   //<-- this is the layout I want to hide
     <TextView/>
     <GridView/>
  </LinearLayout>
</LinearLayout>

And this is the onClick method which I've set up in MyArrayAdapter (used to inflate buttons)

@Override
public void onClick(View v) {
   View convertView = activity.getLayoutInflater().inflate(R.layout.layout_home, null);  
   LinearLayout ll_options = (LinearLayout) convertView.findViewById(R.id.ll_options);
   ll_options.setVisibility(View.INVISIBLE);
}

I think it should work but when I test it, nothing happens.

I found a similar question but it doesn't solve my problem.

4

3 回答 3

8

你为什么在这里夸大布局?:

View convertView = activity.getLayoutInflater().inflate(R.layout.layout_home, null);

做就是了:

View v = activity.findViewById(R.id.ll_options);
v.setVisibility(View.INVISIBLE);
于 2012-04-19T22:44:34.103 回答
2

您创建一个不在可见视图层次结构中的新视图,直到将其添加到那里,然后将其隐藏。所以你隐藏了一些看不见的东西。

相反,请尝试:

@Override
public void onClick(View v) {
   findViewById(R.id.ll_options).setVisibility(View.INVISIBLE);
}

IMO应该工作。它在您的活动的可见(全局)视图层次结构中搜索ll_options视图并将其隐藏。

于 2012-04-19T22:45:11.880 回答
0

编辑:

你的按钮在哪里?它在同一个布局文件中吗?您膨胀了一个新布局并在那里隐藏了 LinearLayout,但从未使用过这个新布局。确保您可以访问侦听器中的 contentView。

于 2012-04-19T22:42:19.113 回答