【问题标题】:Custom linear layout having problems with adding views自定义线性布局在添加视图时遇到问题
【发布时间】:2015-02-17 22:35:54
【问题描述】:

我有这个代码:

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));
    }
}

问题是,当我执行它时,我只得到绿色方块(正如我计划的那样),但没有任何布局应该在里面并创建一个漂亮的网格。你知道为什么吗?我尝试在课堂之外添加文本字段,但也完全失败了。

只是一些启动代码来告诉你到底发生了什么:

package com.shipwreckt;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class Shipwreckt extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_shipwreckt);
        GridBoard gridBoard=new GridBoard(this);

        TextView tv=new TextView(this);
        tv.setText("Problem");
        gridBoard.addView(tv);
        setContentView(gridBoard);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_shipwreckt, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

  • 我想学习一些新东西,如果我想实现 gridlayout,我也会遇到同样的情况。
  • 我还没有遇到过需要实现自己的布局的情况,即使您这样做了,您也应该进行最小的更改。布局旨在由其他实体操作,而不是您的代码的基础。我的建议是制作一个管理 GridLayout 的 GridBoard 对象,而不是试图取代它。这是更好的编码实践和更有用的学习技能。

标签: java android android-linearlayout android-custom-view


【解决方案1】:

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

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

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

现在完美运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    • 2020-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多