我有这段代码:
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);
}
}
答案 0 :(得分:1)
我意识到onMesure做了更复杂的计算,所以我修改了它:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure( (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec) , (widthMeasureSpec>heightMeasureSpec?heightMeasureSpec:widthMeasureSpec));
}
现在它完美无缺