【问题标题】:Android custom LinearLayout : getParams return nullAndroid自定义LinearLayout:getParams返回null
【发布时间】:2016-03-15 22:51:56
【问题描述】:

我尝试创建一个像网格一样的自定义 LinearLayout。我成功地做到了,并获得了我想要的方面,但我仍然有问题。当我只有一排时。我想将自定义布局的 layout_height 属性从 wrap_content 更改为 ma​​rch_parent 以便能够将要创建的唯一 LinearLayout 居中。

所以现在我的代码是在 onFinishInflate 方法之后执行的,但这里有一个问题,我无法获得 LayoutParams,因为该值为 null 并且当我尝试设置自己的新参数时参数,它不起作用:

setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

这是我的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;

public class GameGridLayout extends LinearLayout {
    private Context context;
    private int currentRow = -1, maxRowItems = 3;
    private ArrayList<LinearLayout> layouts = new ArrayList<>();

    public GameGridLayout(Context context) {
        super(context);
        this.context = context;
    }

    public GameGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public GameGridLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    private ArrayList<View> getViews() {
        int nbChilds = getChildCount();
        ArrayList<View> views = new ArrayList<>();
        for (int i = 0; i < nbChilds; i++) {
            views.add(getChildAt(i));
        }
        return views;
    }

    private void reorganizeViews(Context context) {
        ArrayList<View> views = getViews();
        if(views.size() == 4) maxRowItems = 2;

        for (int i = 0; i < views.size(); i++) {
            if(i % maxRowItems == 0) {
                LinearLayout currentLayout = new LinearLayout(context);
                layouts.add(currentLayout);
                currentRow++;
            }
            View currentChild = views.get(i);
            if(currentChild != null) {
                removeView(currentChild);
                layouts.get(currentRow).addView(currentChild);
            }
        }

        for (int i = 0; i < layouts.size(); i++) {
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            layouts.get(i).setLayoutParams(params);
            layouts.get(i).setOrientation(HORIZONTAL);
            layouts.get(i).setWeightSum(3);
            layouts.get(i).setGravity(Gravity.CENTER);

            addView(layouts.get(i));
        }

        setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        reorganizeViews(context);
    }
}

如果你知道该怎么做,我看到如果我使用 inflate 方法,我可能会得到我想要的结果,但我不知道该怎么做,例如:

inflate(context, ??, this);

我没有要膨胀的 xml,所以...

【问题讨论】:

    标签: android android-layout android-linearlayout android-custom-view layoutparams


    【解决方案1】:

    onFinishInflate 中调用setLayoutParams 实际上不会起作用,因为您的GameGridLayoutLayoutParams 稍后添加到其父级时将被覆盖。

    我建议您在将GameGridLayout 添加到其父级后调用setLayoutParams

    【讨论】:

    • 是的,我已经完成了,只需创建一个简单的方法 setLayoutParams 并在我的 onCreate 中调用该方法。它的优点是容易^_^。谢谢
    猜你喜欢
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-21
    相关资源
    最近更新 更多