我有这样的布局
main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:alignParentTop ="true"
android:scrollbars="none" >
<LinearLayout
android:id="@+id/linearLayout4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/btnLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:text="Login" />
<RelativeLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:visibility="gone" >
<Button
android:id="@+id/btnEditUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtLoggedInfo"
android:layout_centerInParent="true"
android:layout_marginTop="5dip"
android:text="Edit User" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
我的自定义视图是
public class MyView extends RelativeLayout {
private Context context = null;
private TextView txtLoggedInfo;
private View contentView = null;
public MyView(Context context) {
super(context);
this.context = context;
getLayout();
}
private void getLayout() {
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
contentView = layoutInflater.inflate(R.layout.main, null);
this.addView(contentView);
//......
if (bool ) {
((Button)findViewById(R.id.btnLogin)).setVisibility(View.GONE);
((RelativeLayout) findViewById(R.id.layout)).setVisibility(View.VISIBLE);
} else {
((Button)findViewById(R.id.btnLogin)).setVisibility(View.VISIBLE);
((RelativeLayout) findViewById(R.id.layout))
.setVisibility(View.GONE);
}
}
}
我想点击“登录”按钮开始新活动,登录后返回MyView。我用过
RelativeLayout.LayoutParams layputParams = new Relative.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
this.addContentView(new MyView(this), layputParams);
在活动内部返回活动。这样做时,视图可见性不会改变。这里的问题是什么,我该如何解决这个问题?
由于
答案 0 :(得分:1)
我强烈建议你这样做
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if (bool ) {
((Button)findViewById(R.id.btnLogin)).setVisibility(View.GONE);
((RelativeLayout) findViewById(R.id.layout)).setVisibility(View.VISIBLE);
} else {
((Button)findViewById(R.id.btnLogin)).setVisibility(View.VISIBLE);
((RelativeLayout) findViewById(R.id.layout))
.setVisibility(View.GONE);
}
super.onWindowFocusChanged(hasFocus);
}
这意味着如果Windows焦点已更改,则已创建所有内容。在之前的代码中,条件可能无法正常运行,因为尚未加载所有内容。