半吊子新手想写一个游戏里面物品栏一样的东西,结果在画格子的时候遇到了问题。。。求大神点醒我。
这是view的代码,将此代码直接放入mainactivity主函数,view的显示没有问题
LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
final View mview = layoutInflater.inflate(R.layout.inventory, null);
RelativeLayout mRalativeLayout = (RelativeLayout) findViewById(R.id.mainRalativeLayout);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(-1, -1);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mRalativeLayout.addView(mview);
mview.setLayoutParams(layoutParams);
但是将上述代码放入点击事件之后,我试过了点击事件,还有handler,也试过了直接在点击事件里面用setContentView加载布局。view之中的小方块都不能显示了。
以下是相关的代码
public class InventoryView extends GridLayout {
public int cardWidth;
public InventoryView(Context context) {
super(context);
set();
}
public InventoryView(Context context, AttributeSet attrs) {
super(context, attrs);
set();
}
public InventoryView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
set();
}
private Cell[][] cellsMap = new Cell[9][9];
private void set() {
setBackgroundColor(0xffbbada0);
setColumnCount(9);
}
public void addCell(int CellWidth, int CellHeight) {
Cell cell;
int ID = 0;
for (int i = 0; i < 9; i++) {
for (int k = 0; k < 9; k++) {
ID++;
cell = new Cell(getContext());
cell.setCellID(ID);
final String finalID = ID + "";
cell.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.v("ID=", finalID);
}
});
addView(cell, CellWidth, CellHeight);
cellsMap[i][k] = cell;
}
}
}
public int getCardWidth() {
return cardWidth;
}
public void setCardWidth(int cardWidth) {
this.cardWidth = cardWidth;
}
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
cardWidth = (Math.min(w, h) - 10) / 9;
addCell(cardWidth, cardWidth);
}
}
public class Cell extends FrameLayout {
private TextView textView;
private int CellID;
public Cell(Context context) {
super(context);
CellID = -1;
textView = new TextView(getContext());
textView.setTextSize(16);
textView.setGravity(Gravity.CENTER);
textView.setBackgroundColor(0x33ffffff);
LayoutParams lp = new LayoutParams(-1,-1);
lp.setMargins(5,5,0,0);
addView(textView,lp);
}
public TextView getTextView() {
return textView;
}
public void setTextView(TextView textView) {
this.textView = textView;
}
public int getCellID() {
return CellID;
}
public void setCellID(int cellID) {
CellID = cellID;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.example.wang123456789pei.inventory.InventoryView
android:id="@+id/InventoryViewID"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@color/colorPrimary">
</com.example.wang123456789pei.inventory.InventoryView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/cancleBt"
android:text="关闭"/>
</LinearLayout>
mRalativeLayout.addView(mview);
mview.setLayoutParams(layoutParams);
这两句代码是不是该交换下位置?