15

我发现自己需要完全在 Java 中创建一个视图,而不知道父级是什么具体类型。

例子:

public View getView(int position, View convertView, ViewGroup parent){
    if(null == convertView){
        convertView = new TextView(parent.getContext());
    }
    ((TextView) convertView).setText(getItem(position).getName());
}

现在假设我想更改它,以便 convertView 在两个方向上都是 wrap_content。由于这是一个适配器,我想避免将适配器与父级的具体类型耦合,但我在 setLayoutParams() 中给它的 LayoutParams 必须是正确的具体类型,否则应用程序将崩溃(即,如果父级是ListView 必须是 ListView.LayoutParams,如果是 LinearLayout,则必须是 LinearLayout.LayoutParams 等)。我也不想使用 switch 语句,因为这只是一种更灵活的耦合形式,如果我将此适配器附加到视图,我没想到我仍然会崩溃。有没有通用的方法来做到这一点?

4

4 回答 4

10

您可以使用以下代码执行此操作:

LayoutParams params = parent.generateLayoutParams(null);

编辑:上面的方法不起作用,因为ViewGroup.generateLayoutParams()需要android:layout_widthandroid:layout_height在 pass 中设置AttributeSet

如果您使用ViewGroup.LayoutParams任何布局,那么一切都会正常工作。但是如果你使用LinearLayout.LayoutParamswithRelativeLayout例如,那么就会抛出异常。

编辑:这个问题有一个我不太喜欢的可行解决方案。generateLayoutParams()解决方案是使用 valid调用AttributeSetAttributeSet您可以使用至少两种不同的方法来创建对象。我已经实现了其中之一:

资源\布局\params.xml

<?xml version="1.0" encoding="utf-8"?>

<view xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="20dip" />

SomeActivity.java

private void addView(ViewGroup viewGroup, View view) {
    viewGroup.addView(view);
    view.setLayoutParams(generateLayoutParams(viewGroup));
}

private ViewGroup.LayoutParams generateLayoutParams(ViewGroup viewGroup) {
    XmlResourceParser parser = getResources().getLayout(R.layout.params);
    try {
        while(parser.nextToken() != XmlPullParser.START_TAG) {
            // Skip everything until the view tag.
        }
        return viewGroup.generateLayoutParams(parser);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

创建AttributeSet对象的另一种方法是实现AttributeSet接口并使其返回android:layout_widthandroid:layout_height以及您需要的其他布局属性。

于 2011-08-10T21:53:22.770 回答
2

我有以下解决方法:

View view = new View(context);
parent.addView(view);

LayoutParams params = view.getLayoutParams();
//Do whatever you need with the parameters
view.setLayoutParams(params);

除非您做一些hacky,否则您无法LayoutParams自己自动生成正确的,因此您应该创建一个将为您自动生成它们的情况:只需将视图添加到容器中。之后,您可以从视图中获取它们并执行您需要的操作。

唯一需要注意的是,如果您不需要自己将视图添加到容器中,则稍后必须从中删除视图,但这应该不是问题。

于 2014-12-11T21:11:23.520 回答
1

为什么没有人(尚未 -> 参见 2016.05)在这里提到基于反射的方法?

1.入口点:

 /**
 *  generates default layout params for given view group 
 *  with width and height set to WLayoutParams.RAP_CONTENT 
 *
 * @param viewParent - parent of this layout params view 
 * @param <L> - layout param class 
 * @return layout param class object 
 * @throws NoSuchMethodException
 * @throws InvocationTargetException
 * @throws IllegalAccessException
 */
@NonNull
private <L extends ViewGroup.LayoutParams> L generateDefaultLayoutParams(@NonNull ViewGroup viewParent)
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Method generateDefaultLayoutParamsMethod = ViewGroup.class.getDeclaredMethod("generateDefaultLayoutParams");
      // caution: below way to obtain method has some flaw  as we need traverse superclasses to obtain method in case we look in object and not a class             
      // = viewParent.getClass().getDeclaredMethod("generateDefaultLayoutParams");
    generateDefaultLayoutParamsMethod.setAccessible(true);
    return (L) generateDefaultLayoutParamsMethod.invoke(viewParent);
}

2. 用途:

@NonNull
protected <T extends ViewGroup.LayoutParams> T createLayoutParamsForView(ViewGroup viewParent,
                                                                         @IdRes int belowViewId)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    return createLayoutParamsForView(null,null,viewParent,belowViewId);
}

@NonNull
protected <T extends ViewGroup.LayoutParams> T createLayoutParamsForView(@NonNull Context context,
                                                                         @NonNull Class<? extends ViewGroup> parentClass,
                                                                         @IdRes int belowViewId)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    return createLayoutParamsForView(context,parentClass,null,belowViewId);
}

@NonNull
private <T extends ViewGroup.LayoutParams> T createLayoutParamsForView(@Nullable Context context,
                                                                       @Nullable Class<? extends ViewGroup> parentClass,
                                                                       @Nullable ViewGroup parent,
                                                                       @IdRes int belowViewId)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    if(context == null && parent == null) throw new IllegalStateException("either context and parent class or must be non null!");
    T layoutParams = (T) (parent != null ? generateDefaultLayoutParams(parent) : generateDefaultLayoutParams(context, parentClass));
    if (belowViewId != NO_ID  && RelativeLayout.LayoutParams.class.isAssignableFrom(layoutParams.getClass())){
        ((RelativeLayout.LayoutParams)layoutParams).addRule(RelativeLayout.BELOW, belowViewId);
    }
    return layoutParams;
}

@NonNull
private <P extends ViewGroup> P instantiateParent(Class parentClass, Context context) 
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
    Constructor constructor = parentClass.getDeclaredConstructor(Context.class);
    constructor.setAccessible(true);
    return (P) constructor.newInstance(context);
}

@NonNull
private <L extends ViewGroup.LayoutParams, P extends ViewGroup> L generateDefaultLayoutParams(Context context, @NonNull Class<? extends ViewGroup> viewParentClass) 
        throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
    P viewParent = instantiateParent(viewParentClass, context);
    return generateDefaultLayoutParams(viewParent);
}
于 2016-05-08T02:13:15.150 回答
0

所有LayoutParams类都有一个通用超类:ViewGroup.LayoutParams. 并且所有流行的布局(FrameLayoutLinearLayoutConstraintLayout等)都ViewGroup.MarginLayoutParams用作其各自类的基LayoutParams类。

因此,如果您只需要宽度、高度和边距,您可以创建ViewGroup.MarginLayoutParams并将其作为布局参数传递给ViewGroup. 然后会发生的是容器将ViewGroup.MarginLayoutParams使用protected LayoutParams ViewGroup#generateLayoutParams(ViewGroup.LayoutParams p).

此代码适用于任何container类,包括LinearLayout,RelativeLayout等:

val layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
container.addView(view, layoutParams)
于 2019-03-15T10:46:33.530 回答