简体   繁体   English

以编程方式将子级添加到GridLayout

[英]Programmatically add child to GridLayout

I made a custom View , now i want to add it to a GridLayout in a programmatically way but i've no idea how to do that... 我做了一个自定义的View,现在我想以编程方式将其添加到GridLayout,但是我不知道该怎么做...

Activity : 活动内容:

public class FullScreenGame extends Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.full_screen_layout);
    GridLayout grid = (GridLayout) findViewById(R.id.cell_grid);
    grid.addView(new CustomCell(this));

  }
}

i'm trying to do like that but nothing is displayed. 我正在尝试那样做,但什么也没显示。 Custom cell class: 自定义单元格类别:

public class CustomCell extends View{

private Paint shadowPaint , ovalColor;
private int ypad =0, xpad =0, height =150, width = 150;
//TODO calculate spacing properly

public CustomCell(Context context){
    super(context);

}

public CustomCell(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomCell,0,0);

    ypad = a.getInteger(R.styleable.CustomCell_ypad,0);
    xpad = a.getInteger(R.styleable.CustomCell_xpad,0);




    init();
}


@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(width+10,height+10);
}

private void init(){
    shadowPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    shadowPaint.setColor(Color.BLACK);
    shadowPaint.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));

    ovalColor= new Paint(Paint.ANTI_ALIAS_FLAG);
    ovalColor.setColor(Color.WHITE);
    ovalColor.setMaskFilter(new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL));

}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawOval(xpad    , ypad    , height+5, width+5, shadowPaint);
    canvas.drawOval(xpad+5 , ypad +5, height, width, ovalColor  );



}
}

full_screen_layout.xml: full_screen_layout.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#1565c0"
    android:rowCount="5"
    android:columnCount="5" 
    android:id="@+id/cell_grid">

    </GridLayout>

I'm trying to create a game board made with Custom cells , but i can't figure the easy / correct way to perform it , also how can i add view to a specific row and column , it could help a lot. 我正在尝试创建一个使用“自定义”单元格制作的游戏板,但是我无法确定简单/正确的执行方法,也无法将视图添加到特定的行和列,这可能会很有帮助。

Try this, 尝试这个,

GridLayout layout = (GridLayout) findViewById(R.id. cell_grid); 
layout.setUseDefaultMargins(true); 
layout.setAlignmentMode(ALIGN_BOUNDS); 
layout.setRowOrderPreserved(false); 
layout.setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 

layout.addView(childView, new GridLayout.LayoutParams(row, col));
// 'childView' can be any View object

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以编程方式设置 GridLayout 子级的 rowSpan 或 colSpan? - Set rowSpan or colSpan of a child of a GridLayout programmatically? 如何以编程方式使用LinearLayout子元素在Gridlayout中设置Colspan? - How to set Colspan in Gridlayout using LinearLayout Child elements programmatically? 如何以编程方式添加GridLayout子项的layout_columnWeight? - How to set layout_columnWeight for a GridLayout child when added programmatically? BaseExpandableListAdapter以编程方式添加子级 - BaseExpandableListAdapter add child programmatically 以编程方式将视图添加到 Xamarin.Android C# 中的 GridLayout - Programmatically add view to a GridLayout in Xamarin.Android C# 以编程方式在RelativeLayout中添加子视图 - Add child views in RelativeLayout programmatically 如何以编程方式将app:layout_columnWeight和app:layout_rowWeight设置为GridLayout的子级 - how to set app:layout_columnWeight and app:layout_rowWeight to GridLayout's child programmatically GridLayout子节点的getLocationOnScreen - getLocationOnScreen of a child of GridLayout Android gridlayout以编程方式使用colspan和rowspan - Android gridlayout programmatically with colspan and rowspan 如何以编程方式为 GridLayout 创建按钮 - How create a button by programmatically for a GridLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM