我希望从班级
更改视图linearlayout
但不能使用此代码
main.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb();
}
}
并且:
ComNet.java
public class ComNet {
public static Context context;
public static void readDb() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewMain = inflater.inflate(R.layout.main, null);
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
lnrPart.setVisibility(View.GONE);
}
}
如何在班级lnrPart
)中main.xml
更改ComNet
?
答案 0 :(得分:3)
View viewMain = inflater.inflate(R.layout.main, null);
使用此行,您将创建一个新的View
,其内容为main.xml
。此对象与您在MainActivity
屏幕上看到的对象不同。 setVisibility
正在发挥作用。你在错误的实例上调用它
答案 1 :(得分:2)
将活动rootView发送到readDB函数。
main.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb(findViewById(R.id.main));
}
}
ComNet.java
public static void readDb(View viewMain) {
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
viewMain.setVisibility(View.GONE);
}