我想,以取代其他其他通过代码视图
例如,我需要改变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
}
Run Code Online (Sandbox Code Playgroud)
我需要使用这么多地方.我知道通过发送父母来设置viewgroup.
无论如何viewgroup要从父母那里得到imageview
感谢
bri*_*tzl 10
这是微不足道的,其他海报已经与解决方案联系或暗示.您可以通过View.getParent()获取View的父级.您需要将返回的值转换为ViewGroup(q: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);
}
}
Run Code Online (Sandbox Code Playgroud)
需要考虑的是,当您将一个视图替换为另一个视图时,您将失去相对布局中的任何定位.对此的一个解决方案是确保要替换的视图包装在另一个视图中,并且包装容器视图是位于相对布局中的视图.
| 归档时间: |
|
| 查看次数: |
10369 次 |
| 最近记录: |