【问题标题】:Replace a view with other view By code用其他视图替换视图通过代码
【发布时间】:2011-07-07 03:39:43
【问题描述】:


我想通过代码将视图替换为其他视图
例如,我需要通过代码将imageview 更改为progressBar

public void changeToProgressBar(ImageView imageview,Context context){
            ProgressBar one = new ProgressBar(context);
            one.setLayoutParams(imageview.getLayoutParams());
            imageview.setVisibility(View.GONE);
            one.setVisibility(View.VISIBLE);

            //NOW I NEED TO PLACE 'one' EXACTLY THE PLACE WHERE  'imageview' WAS THERE 
        }

我需要使用这么多地方。我知道通过发送父 viewgroup 来设置。
有没有办法从imageview得到父viewgroup
谢谢

【问题讨论】:

  • 问了同样的问题here :)

标签: android


【解决方案1】:

为此,请同时使用imageviewprogressBar,并根据您的要求设置可见性。

例如

如果您想让progressBar 可见,请将setvisibility(Visibility.GONE) 放入imageview 并将setvisibility(Visibility.VISIBLE) 放入progressBar

【讨论】:

  • 感谢您的回答。我知道,但为此我需要在我的 xml 中声明进度条。这里我通过代码创建它
  • yourview.removeView(imageview) 和 youtview.addView(progressbar)
  • 如何从我的imageview 获取yourview
【解决方案2】:

正如 User333 所描述的那样,这可能是一种解决方案。 也可以通过调用yourview.removeView(imageview) 删除图像视图,然后创建进度条并将其放在视图中,而不是通过yourview.addView(progressbar)

【讨论】:

  • 如何从我的imageview 获取yourview
  • 'yourview',是包含你的'imageview'的视图。例如。 'LinearLayout' 或其他视图?
  • 您也可以这样做,imageview.getParent() 返回包含您的 imgageview 的视图..
  • 没有。 getParent 只会返回视图而不是父视图组。我们无法将视图添加到另一个视图组
【解决方案3】:

通过从活动级别调用 findViewById() 来检索您要更改的视图
http://developer.android.com/reference/android/app/Activity.html#findViewById(int)
然后找到您要更改的子视图
http://developer.android.com/reference/android/view/View.html#findViewById(int)
然后使用http://developer.android.com/reference/android/view/ViewGroup.html 中的函数来操作视图。

【讨论】:

    【解决方案4】:

    您可以从任何其他简单的 java 类或其他活动中更改 Android 活动视图。
    您只需要传递当前视图并通过要更改的视图获取您的元素

    As :

    public class MainActivity extends Activity{
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        // Setting UI
        View currentView = getWindow().getDecorView().getRootView();
    
        //showHeaderLayout is simple java class or it can be any activity
        changeLayout.setView(currentView);
    
    }
    


    类:changeLayout

     public class changeLayout{
        public static View setView(final Activity activity, final View myView)
         {
          // myView helps to get Activity view , which we want to change.
          TextView tv = (TextView) myView.findViewById(R.id.tv); 
          tv.setText("changs Text Via other java class !!");
         }
      }
    

    注意:传递视图使您能够更改 Activity 之外的任何 Activity 视图。

    【讨论】:

      【解决方案5】:

      这很简单,其他海报已经链接或暗示了解决方案。您可以通过 View.getParent() 获取 View 的父级。您需要将返回的值转换为 ViewGroup(问:View 的父级可以是 ViewGroup 以外的任何东西吗?)。将父视图作为 ViewGroup 后,您可以执行 ViewGroup.removeChild(viewToRemove) 删除旧视图并使用 ViewGroup.addChild(viewToAdd) 添加新视图。

      您可能还希望将新视图添加到与删除视图相同的索引处,以确保不会将新视图置于其他视图之上或之下。这是一个完整的实用程序类供您使用:

      import android.view.View;
      import android.view.ViewGroup;
      
      public class ViewGroupUtils {
      
          public static ViewGroup getParent(View view) {
              return (ViewGroup)view.getParent();
          }
      
          public static void removeView(View view) {
              ViewGroup parent = getParent(view);
              if(parent != null) {
                  parent.removeView(view);
              }
          }
      
          public static void replaceView(View currentView, View newView) {
              ViewGroup parent = getParent(currentView);
              if(parent == null) {
                  return;
              }
              final int index = parent.indexOfChild(currentView);
              removeView(currentView);
              removeView(newView);
              parent.addView(newView, index);
          }
      }
      

      需要考虑的是,当您将一个视图替换为另一个视图时,您将失去在相对布局中的任何定位。对此的一种解决方案是确保您要替换的视图被包装在另一个视图中,并且被包装的容器视图是位于相对布局中的视图。

      【讨论】:

        【解决方案6】:

        我认为以下应该是最好的方法,因为我们不需要设置布局参数。

        setvisibility(Visibility.GONE)
        setvisibility(Visibility.VISIBLE)
        

        【讨论】:

          【解决方案7】:

          我相信以下链接不仅可以帮助您,还可以向您展示您的需求方向。 https://stackoverflow.com/a/3760027/3133932

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-06-18
            • 1970-01-01
            • 1970-01-01
            • 2015-07-24
            • 2021-08-02
            • 1970-01-01
            • 2010-09-10
            • 1970-01-01
            相关资源
            最近更新 更多