Android可视性和布局GONE VISIBLE循环重叠布局

时间:2012-12-09 06:31:04

标签: android visibility relativelayout cycle visible

如果你不介意看视频

http://www.youtube.com/watch?v=kWFsmeBJ4l0

我有一个父级布局:相对布局,3个线性布局作为子级:标题,菜单和内容。菜单被操作为GONE onCreate,然后在特定事件后可见,并在事件的相反后再次GONE。问题出现在GONE VISIBLE GONE的第一个循环之后,再次执行GONE将破坏用户界面,然后内容与菜单重叠。谁能告诉我发生了什么?

这是布局的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/metro_dark_blue"
    android:clipChildren="true" >

    <LinearLayout
        android:id="@+id/titleBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/header_gradient"
        android:orientation="horizontal"
         >

        <LinearLayout
            android:id="@+id/collaborationButton"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/collaboration_button_xml"
            android:clickable="true" >
        </LinearLayout>
    </LinearLayout>

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/titleBar"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:background="@null"
        android:gravity="center_vertical"
        android:text="@string/presento"
        android:textSize="25dp" />

    <LinearLayout
        android:id="@+id/connectButton"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignBottom="@+id/titleBar"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/connect_button_xml"
        android:clickable="true" >
    </LinearLayout>

    <View
        android:id="@+id/titleDropShadow"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/titleBar"
        android:background="@drawable/drop_shadow"
        android:orientation="horizontal" />

    <LinearLayout
        android:id="@+id/menuBar"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/titleDropShadow"
        android:background="@drawable/header_gradient"
        android:baselineAligned="true"
        android:visibility="visible" >

        <LinearLayout
            android:id="@+id/remote_button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@drawable/menu_button_xml"
            android:clickable="true"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView_remote"
                android:layout_width="85.5dp"
                android:layout_height="58.5dp"
                android:background="@null"
                android:contentDescription="@null"
                android:src="@drawable/remote_control_button" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/file_manager_button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@drawable/menu_button_xml"
            android:clickable="true"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView_file_manager"
                android:layout_width="85.5dp"
                android:layout_height="58.5dp"
                android:background="@null"
                android:contentDescription="@null"
                android:src="@drawable/file_manager_button" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/desktop_button"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="0.50"
            android:background="@drawable/menu_button_xml"
            android:clickable="true"
            android:gravity="center_horizontal" >

            <ImageView
                android:id="@+id/imageView_desktop"
                android:layout_width="85.5dp"
                android:layout_height="58.5dp"
                android:background="@null"
                android:contentDescription="@null"
                android:src="@drawable/desktop_mode_button" />
        </LinearLayout>
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/fragment_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="false"
        android:layout_below="@+id/menuBar"
        android:baselineAligned="true"
        android:orientation="vertical"
        android:visibility="visible" >

    </RelativeLayout>


</RelativeLayout>

这是处理可见性的代码

public void connectService(String ip, String hostType, String name) {
    server = new ConnectedServer(ip, hostType, "Presento");
    Toast.makeText(getApplicationContext(), "You have been connected to Presento Server with IP: " + ip, Toast.LENGTH_LONG).show();

    //RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) fragmentContainer.getLayoutParams();
    //RelativeLayout.LayoutParams param2 = (RelativeLayout.LayoutParams) belowMenu.getLayoutParams();
    //param2.height = 65;
    //param2.addRule(RelativeLayout.ABOVE, R.id.fragment_container);
    //param2.addRule(RelativeLayout.BELOW, R.id.titleBar);
    belowMenu.setVisibility(View.VISIBLE);
        belowMenu.startAnimation(new MyScaler(1.0f, 1.0f, 0.0f, 1.0f, 500, belowMenu, false));

    //belowMenu.setLayoutParams(param2);
    //params.addRule(RelativeLayout.BELOW,R.id.menuBar);
    //fragmentContainer.setLayoutParams(params);
    //fragmentContainer.setVisibility(View.GONE);
    //fragmentContainer.setVisibility(View.VISIBLE);

}

这是GONE时处理的代码

connectButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //if not yet connected to a server, find a server through jmdns and get the ip
                if (server == null) {
                    //setting device's ip address
                    myIpAddress = getMyIpAddress();
                    InetAddress inet = intToInetAddress(ipStringToInt(myIpAddress));
                    if (myIpAddress.equals("")) alertMessage("You don't seem to be connected to a Wi-Fi Service, please make sure that you are connected and try to connect again.", "Something went wrong");
                    else {
                        new SeekServer(inet, MenuActivity.this).execute(jmdns);
                    }
                }
                //server already connected
                else {
                    FragmentManager FM = getSupportFragmentManager();
                    FragmentTransaction FT = FM.beginTransaction();
                    String sessionID = pref.getString("sessionID", "0");
                    FT.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
                    try {
                    RemoteControlFragment remote = (RemoteControlFragment)FM.findFragmentById(R.id.fragment_container);
                    if (sessionID.equals("0")) {
                        join = new JoinSessionFragment();
                        FT.replace(R.id.fragment_container, join);
                        FT.commit();
                    } else {
                        String accountName = pref.getString("accountName", "");
                        status.setText(accountName);
                        FT.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
                        TutorialFragment tutor = new TutorialFragment();
                        FT.replace(R.id.fragment_container, tutor);
                        FT.addToBackStack(null);
                        FT.commit();
                    }
                    } catch (ClassCastException ex) {
                        try {
                        FileManagerFragment manager = (FileManagerFragment)FM.findFragmentById(R.id.fragment_container);
                        if (sessionID.equals("0")) {
                            join = new JoinSessionFragment();
                            FT.replace(R.id.fragment_container, join);
                            FT.commit();
                        } else {
                            String accountName = pref.getString("accountName", "");
                            status.setText(accountName);
                            FT.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
                            TutorialFragment tutor = new TutorialFragment();
                            FT.replace(R.id.fragment_container, tutor);
                            FT.addToBackStack(null);
                            FT.commit();
                        }
                        }
                        catch (ClassCastException ex2) {

                        }
                    }


                    Toast.makeText(getApplicationContext(), "You have been disconnected from server", Toast.LENGTH_LONG).show();
                    belowMenu.startAnimation(new MyScaler(1.0f, 1.0f, 1.0f, 0.0f, 500, belowMenu, true));

                    server = null;
                        if (jmdns != null) {
                        jmdns.unregisterAllServices();
                        try {
                            jmdns.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }

                }
            }
        });

动画类

public class MyScaler extends ScaleAnimation {

        private View mView;

        private LayoutParams mLayoutParams;

        private int mMarginBottomFromY, mMarginBottomToY;

        private boolean mVanishAfter = false;

        public MyScaler(float fromX, float toX, float fromY, float toY, int duration, View view,
                boolean vanishAfter) {
            super(fromX, toX, fromY, toY);
            setDuration(duration);
            mView = view;
            mVanishAfter = vanishAfter;
            mLayoutParams = (LayoutParams) view.getLayoutParams();
            int height = mView.getHeight();
            mMarginBottomFromY = (int) (height * fromY) + mLayoutParams.bottomMargin - height;
            mMarginBottomToY = (int) (0 - ((height * toY) + mLayoutParams.bottomMargin)) - height;
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (interpolatedTime < 1.0f) {
                int newMarginBottom = mMarginBottomFromY
                        + (int) ((mMarginBottomToY - mMarginBottomFromY) * interpolatedTime);
                mLayoutParams.setMargins(mLayoutParams.leftMargin, mLayoutParams.topMargin,
                    mLayoutParams.rightMargin, newMarginBottom);
                mView.getParent().requestLayout();
            } else if (mVanishAfter) {
                mView.setVisibility(View.GONE);
            }
        }

    }

我首先怀疑动画引起了这种情况,但后来我明显地使用了GONE和VISIBLE而没有动画导致与视频相同的结论。

0 个答案:

没有答案