gpt4 book ai didi

java - 自定义线性布局在添加 View 时有问题

转载 作者:行者123 更新时间:2023-11-30 11:08:24 25 4
gpt4 key购买 nike

我有这个代码:

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

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

只是一些开始的代码来向你展示到底发生了什么:

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

最佳答案

我意识到 onMesure 会做更复杂的计算,所以我修改了它:

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

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

现在完美运行

关于java - 自定义线性布局在添加 View 时有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com