【问题标题】:Suggestion to use layout使用布局的建议
【发布时间】:2013-07-09 18:27:23
【问题描述】:

我在 android 的屏幕上有 3 个视图,假设有一组按钮、地图、列表和另一个信息视图。这些都是垂直的。

所以,如果我点击列表,地图应该得到更新,按钮颜色也应该改变。有时如果我单击按钮信息应该显示。

在这种情况下,使用片段好不好?或相对布局?建议

谢谢

【问题讨论】:

  • 使用片段。一个用于列表,另一个用于显示地图。

标签: android android-layout android-fragments fragment


【解决方案1】:

我会选择一个由RelativeLayout 组成的Activity,在RelativeLayout 内有3 个FrameLayout。然后我会将代码中的片段添加到 FrameLayouts。每个片段 1 个。这样,您可以轻松地以任何您想要的方式移动片段。滑动菜单,顶部滑动,侧滑,上下滑动,如此简单。如果你像这样设置它就很容易了。

When the far left is selected you can shove ther other 2 the right, when the center receives focus slide the left one to the left and right one right, and when the far right one gets focus, slide the other to to the左边。

或者你可以从上到下。 或者您可以始终让所有 3 个空间都相等。 或者你总是可以把不集中的东西推到右、左、上、下,可能性是无穷无尽的。您可以将未聚焦的 2 到 1/4 尺寸缩小,然后将它们推到屏幕的一侧,一个在顶部,一个在底部。

看到我要去哪里了吗?

否则使用 3 个框架布局进行线性布局,并将每个框架布局设置为 weight = 1(可能必须切换其他一些选项以始终保持它们完美),然后添加您的片段。

    public void swapfragment(int fragId, Bundle args, boolean slide)
    {
        FragmentTransaction ft = getFragmentManager().beginTransaction();

        switch (fragId)
        {
        case FRAGID_DEVICE:
            currentFrag = new FragmentDevice();
            currentFrag.setArguments(args);
            ((FragmentDevice) currentFrag).initialize();
            break;
        case FRAGID_NETWORK:
            currentFrag = new FragmentNetwork();
            currentFrag.setArguments(args);
            ((FragmentNetwork) currentFrag).initialize();
            break;
        }

        ft.replace(R.id.flFragHost, currentFrag).commit();
        if (slide)
            slideFragment();
    }

    private void slideFragment()
    {
        final Point displaySize = new Point();
        getWindowManager().getDefaultDisplay().getSize(displaySize);

        if (isFragmentOut)
        {
            isFragmentOut = false;
            Animation slideOutAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_right_out_80);
            AnimationListener listener = new Animation.AnimationListener()
            {

                public void onAnimationStart(Animation animation)
                {
                }

                public void onAnimationRepeat(Animation animation)
                {
                }

                public void onAnimationEnd(Animation animation)
                {
                    int pushback = (int) (displaySize.x * .8f);

                    rlFragHost.clearAnimation();
                    RelativeLayout.LayoutParams FragContainerParams;
                    FragContainerParams = (android.widget.RelativeLayout.LayoutParams) rlFragHost.getLayoutParams();
                    FragContainerParams.setMargins(pushback, 0, pushback * -1, 0);
                    rlFragHost.setLayoutParams(FragContainerParams);
                }
            };
            slideOutAnimation.setAnimationListener(listener);
            rlFragHost.startAnimation(slideOutAnimation);
        }
        else
        {
            isFragmentOut = true;
            Animation slideInAnimation = AnimationUtils.loadAnimation(this, R.anim.slide_left_in_80);
            AnimationListener listener = new Animation.AnimationListener()
            {

                public void onAnimationStart(Animation animation)
                {
                }

                public void onAnimationRepeat(Animation animation)
                {
                }

                public void onAnimationEnd(Animation animation)
                {

                    rlFragHost.clearAnimation();
                    RelativeLayout.LayoutParams FragContainerParams;
                    FragContainerParams = (android.widget.RelativeLayout.LayoutParams) rlFragHost.getLayoutParams();
                    FragContainerParams.setMargins(0, 0, 0, 0);
                    rlFragHost.setLayoutParams(FragContainerParams);
                }
            };
            slideInAnimation.setAnimationListener(listener);
            rlFragHost.startAnimation(slideInAnimation);
        }
    }

我知道这并不完全是您所需要的,但它应该可以帮助您入门。这就是我做滑动菜单的方式。

【讨论】:

  • 好的,你的意思是说保留 3 个片段,将它们以相对布局对齐。相应地使用它。对吗?
  • 正确。这完全取决于您想要什么,但是当我必须为客户做滑动菜单时,我发现这是完成它的最快和最简单的方法。
  • 真的很棒,感谢您的支持。非常感谢
【解决方案2】:

我认为很多都是个人喜好。我特别喜欢的是线性布局。您提到要垂直呈现项目,只需设置 android:orientation="vertical",将项目按顺序放入 xml 文件中,然后就可以了。我个人从来没有使用过片段,所以我不能说它们的用处,但是线性布局还没有让我失望。

【讨论】:

    猜你喜欢
    • 2020-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多