Android 更新现有对象的布局参数

Android 更新现有对象的布局参数,android,view,Android,View,我使用setLayoutParams为自定义视图的对象设置布局参数,如下所示: MyCustomView object = new ObjectView(getApplicationContext()); object.setLayoutParams(new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT,

我使用setLayoutParams为自定义视图的对象设置布局参数,如下所示:

MyCustomView object = new ObjectView(getApplicationContext());
                object.setLayoutParams(new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT));

如何在运行时以编程方式更改或更新同一对象的此LayoutParams?

您可以稍后动态重置或更改参数,如以下代码所示:

MyCustomView object = new ObjectView(getApplicationContext());
object.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));



public void manageViewParams(int width, int height)
{
   ViewGroup.LayoutParams layoutParam = object.getLayoutParams();
   layoutParam.width = width;
   layoutParam.height = height;

   //first check without this below line, it should work. If not then you need to write below line
   object.setLayoutParams(layoutParam);
}

你到底想做什么?你能详细说明一下吗???@Vickyexpert我想再次重置宽度和高度(覆盖原始设置)中没有设置高度或宽度layoutParam@Hussein你必须投下它。例如
ViewGroup.MarginLayoutParams
。这取决于您使用的视图组。如果使用的是LinearLayout,则可以将其强制转换为
LinearLayout.LayoutParams
。这将导致程序崩溃,因为该对象已具有LayoutParams,并且我不想创建新对象
int yourHeight = 200;
int yourWidth = 200;

MyCustomView object = new ObjectView(getApplicationContext());
//this
object.setLayoutParams(new ViewGroup.LayoutParams(
                        yourHeight,
                        yourWidth));
//or this
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                        yourHeight,
                        yourWidth));
object.setLayoutParams(layoutParam);