Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义线性布局在添加视图时遇到问题_Java_Android_Android Linearlayout_Android Custom View - Fatal编程技术网

Java 自定义线性布局在添加视图时遇到问题

Java 自定义线性布局在添加视图时遇到问题,java,android,android-linearlayout,android-custom-view,Java,Android,Android Linearlayout,Android Custom View,我有这个密码: package com.shipwreckt; import android.content.Context; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.LinearLayout; /** * TODO: document your custom view class. */ public class GridBoard extends L

我有这个密码:

package com.shipwreckt;

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


/**
 * TODO: document your custom view class.
 */
public class GridBoard extends LinearLayout {

    Context c;



    public GridBoard(Context context){
        this(context,null);
    }

    public GridBoard(Context context,AttributeSet as){
        super(context,as);
        c=context;
        init();

    }

    void init(){
        this.setBackgroundColor(0xFF006600);
        this.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

        for(int a=0;a<10;a++){
            LinearLayout base=createRow();
                for(int b=0;b<10;b++) {
                    base.addView(createPlace());
                }
            this.addView(base);
        }

    }

    LinearLayout createPlace(){
        LinearLayout ll=new LinearLayout(c){
            public int z;
        };
        ll.setOrientation(HORIZONTAL);
        ll.setPadding(2,2,2,2);
        ll.setLayoutParams(new LayoutParams(0, LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(0xFFFF0000);
        ll.setWeightSum(1);

        return ll;
    }

    LinearLayout createRow(){
        LinearLayout ll=new LinearLayout(c);
        ll.setOrientation(VERTICAL);
        //ll.setWeightSum(1);
        ll.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        ll.setBackgroundColor(0xFFFF0000);
        return ll;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
    }
}

我意识到onMesure会进行更复杂的计算,因此我对其进行了修改:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
}

现在它可以完美地工作了

我不会在这里重新发明轮子,我想学习一些新的东西,如果我想实现gridlayout,我也会遇到同样的情况。我还没有遇到需要实现我自己的布局的情况,即使你这样做了,你也应该进行最小的更改。布局意味着由其他实体操作,而不是代码的基础。我的建议是创建一个GridBoard对象来管理GridLayout,而不是试图取代它。这是更好的编码实践和更有用的学习技能。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
}