【问题标题】:how to send the front image view to the end of elements and so on for framelayout elements android如何将前图像视图发送到元素的末尾等等,用于框架布局元素android
【发布时间】:2016-05-16 20:19:26
【问题描述】:

我创建了一个 FrameLayout 类型的新布局,并添加了图像视图的四个元素我想以编程方式将第一个图像视图带到最后
简而言之,以编程方式重新排列框架布局的元素。假设我们这样做是听按钮 clickListener

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/framelayout">
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091817"
        android:id="@+id/i1"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091819"
        android:id="@+id/i2"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091821"
        android:id="@+id/i3"/>
    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@mipmap/a20141008_091823"
        android:id="@+id/i4"/>

    </FrameLayout>

感谢任何帮助。

【问题讨论】:

    标签: android


    【解决方案1】:

    在这种情况下,您似乎想要重新排序FrameLayout 的子视图。我建议您只需使用ViewGroup,并使用getChildAt(some-index)、addView(viewToAdd, index-to-place-it) 以及removeViewAt(some-index) 来实现重新排序;在我建议的解决方案中 - 我假设布局不会改变(这可以改进):

      ViewGroup viewGroup = (ViewGroup)this.findViewById(android.R.id.content).getRootView();
    
            int first = 0;
            int second = 1;
            int third = 2;
            int fourth = 3;
    
            View view1 = viewGroup.getChildAt(first);
            View view4 = viewGroup.getChildAt(fourth);
            viewGroup.removeViewAt(first);
            //move fourth to first
            viewGroup.addView(view4, first);
            viewGroup.removeViewAt(fourth);
            //move first to fourth
            viewGroup.addView(view1, fourth);
    
            View view2 = viewGroup.getChildAt(second);
            View view3 = viewGroup.getChildAt(third);
    
            viewGroup.removeViewAt(second);
            //move third to second
            viewGroup.addView(view3, second);
            //move second to third
            viewGroup.removeViewAt(third);
            viewGroup.addView(view2, third);
    

    我希望这对您和其他人有所帮助。您还可以在this closely related questionthis one 中查看所选答案,the selected answer here 显示如何重新排序视图。

    【讨论】:

    • 从前到尾的交换在哪里?
    • 好的,我认为代码示例可以帮助您入门。让我添加您重新订购商品的部分。一秒钟……
    • 更新了答案,请查看并告诉我是否可行。
    • 首先非常感谢兄弟。你的方式它可以工作但很抱歉这不是我的问题因为我正在寻找如何使用我找到的这种方式的框架布局做你的相同工作并且它工作正常 frameLayout.bringChildToFront(frameLayout.getChildAt(1));
    • 我知道那个兄弟。感谢您的关注,这只是建议。确定我会给你发正确的答案等一下
    【解决方案2】:

    首先。非常感谢每一位尝试帮助我的人。 然后我以良好的实践方式找出我的问题,我希望与您分享它,可能有人将来需要它。 步骤

    1. 在我的问题中的 xml 中创建一个 frameLayout
    2. 将图像添加到 frameLayout
    3. 创建一个java文件并将其连接到xml
    4. 我们必须知道进入框架布局的所有图像的id
    5. 创建一个计时器,它会在 X 秒内改变图像的顺序
    6. 获取所有图片的数量进入framLayout
    7. 创建 Intager var,它将确定下一个要放在前面的图像
    8. 仍然像我提到的那样调用定时器工作 x 次
    9. 创建方法来停止计时器在我的情况下,我在 onPause() 方法中停止计时器

      这是执行上述所有步骤的 java 文件。希望它可以帮助任何人

    公共类 FrameLayout5 扩展 AppCompatActivity {

    FrameLayout frameLayout; 
    Handler handler;             // this class will work as timer for change the order of images in delay 
    List<Integer> views;    //  this Array list will store all the ids of all the ImageViews into framelayout 
    int counter;        //  this counter will help us to find the next element to bring it to front 
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_layout5);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        frameLayout = (FrameLayout) findViewById(R.id.framelayout);
        handler = new Handler();
        views = new ArrayList<Integer>();
        counter=0;
    
        // get all the children of framelayout 
        for (int i = 0; i < frameLayout.getChildCount() ; i++) {
            views.add(frameLayout.getChildAt(i).getId());
        }
    
        handler.postDelayed(runnable, 1000); // 1000 mean that this method will excute every 1 second 
    
    
    }
    
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
    
            int i=counter%(frameLayout.getChildCount());    
                counter++;
            ImageView a = (ImageView) findViewById(views.get(i));
    
            frameLayout.bringChildToFront(a);
    
            handler.postDelayed(runnable, 1000);
            Log.d("run", "called "+i);
        }
    };
    
    @Override
    protected void onStop() {
        super.onStop();
        handler.removeCallbacks(runnable);
    }
    

    }

    @ExpensiveBelly @ishmaelMakitla

    【讨论】:

      【解决方案3】:

      如何将 ImageView 的可见性更改为不可见/消失,以便只显示要显示的那个?

      【讨论】:

      • 我想过这个问题,但我认为这不是一个好的做法,并且在框架布局中我已经读到可以重新排列它的视图。顺便谢谢你的回复
      • 嗨。你可以看到我是如何解决我的问题的。非常感谢您的帮助。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2018-09-15
      相关资源
      最近更新 更多