1

我有这样的布局

主要的.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);

在活动中返回活动。这样做时,视图可见性不会改变。这里有什么问题,我该如何解决这个问题?

谢谢

4

1 回答 1

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);
}

这意味着如果窗口焦点发生了变化,则所有内容都已创建。在您之前的代码中,条件可能不起作用,因为尚未加载所有内容。

于 2013-01-28T09:52:11.860 回答