android - 如何更改线性布局中 View 的位置。?

标签 android

所以我有一个已经填充了子项的线性布局。有没有办法改变其中一个 child 所在的位置?

如果有任何帮助,我正在尝试交换他们之间的观点。

final LinearLayout parrent = (LinearLayout)findViewById(R.id.llWidgetScreen);
    final LinearLayout Delailah = new LinearLayout(this);
    Delailah.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.view.ViewGroup.LayoutParams.FILL_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
    AppWidgetHostView wedgy = attachWidget(mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo));
    Delailah.addView(wedgy);
    final Button btn = new Button(this);
    btn.setLayoutParams(new android.widget.LinearLayout.LayoutParams((int)(20*scale +0.5f), android.view.ViewGroup.LayoutParams.FILL_PARENT, 0f));
    btn.setOnLongClickListener(new OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            parrent.removeView(Delailah);
            return false;
        }
    });
    btn.setBackgroundColor(mainColor);
    btn.setText(parrent.getChildCount()+1+"");
    btn.setTextColor(textColor);
    btn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(ReadyForDrag==0)
            {
                btn.setBackgroundColor(actiColor);
                ReadyForDrag++;
                DragPosition1=Integer.parseInt(btn.getText().toString());
            }
            else if(ReadyForDrag==1)
            {
                btn.setBackgroundColor(actiColor);
                ReadyForDrag=0;
                LinearLayout v1 = (LinearLayout)parrent.getChildAt(DragPosition1);
                LinearLayout v2 = (LinearLayout)parrent.getChildAt(Integer.parseInt(btn.getText().toString()));
                //move view 2 to position 1
                //move view 1 to position 2
            }               
        }
    });
    Delailah.addView(btn);
    parrent.addView(Delailah);

最佳答案

您可以使用 ViewGroup.removeView(View)ViewGroup.addView(View child, int index, ViewGroup.LayoutParams params)为了这。

关于android - 如何更改线性布局中 View 的位置。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7504531/

相关文章:

android - 为什么运行时无法识别 "res/values/strings.xml"中定义的字符串

java - Android:获取 C++ std::string 直至 JNIEXPORT 被意外清除

android - android中的g++和ubuntu中的g++有什么区别?

android - 有什么方法可以知道 ListView 何时滚动到顶部?

android - 如何通过其值获取字符串资源名称

android - GCM 注册 ID 为空

android - Firebase Android Studio 找不到 com.google.gsm :google-services:3. 0.0

android - Android 手机上的延迟推送通知

Android Phonegap Compilations 在应用程序底部添加了不应该存在的栏?

java - 从 Thread 设置 imageView