简体   繁体   English

如何初始化GridLayout的子视图

[英]How to Initialize child views for GridLayout

How to initialize child of GridLayout and set onClickListener on the multiple ImageView elements present in GridLayout to go on another activity 如何初始化GridLayout子级并在GridLayout存在的多个ImageView元素上设置onClickListener来进行其他活动

Here is my java code: 这是我的Java代码:

public class ResidentialActivity extends Activity {
    GridView grid;
    ImageView img1;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_residential);
        GridLayout grid= (GridLayout) findViewById(R.id.grid);
    }
}

This works for me. 这对我有用。 I am adding cells to a GridLayout and hooking up OnClick listeners to each. 我将单元格添加到GridLayout并将OnClick侦听器连接到每个。

If you already have populated your GridLayout, you could could use GridLayout.getChildCount() and GridLayout.getChildAt(i) to manipulate each cell to add an OnClick listener. 如果您已经填充了GridLayout,则可以使用GridLayout.getChildCount()GridLayout.getChildAt(i)来操纵每个单元格以添加OnClick侦听器。

You need to first have your ResidentialActivity implement View.OnClickListener, like this: 您首先需要使ResidentialActivity实现View.OnClickListener,如下所示:

public class ResidentialActivity extends AppCompatActivity implements View.OnClickListener {

and override the OnClick() method like: 并重写OnClick()方法,例如:

@Override
protected void onClick(final View view) {
. . .  launch your other activity in here
}

To hook up the onClick you can iterate through the cells (you could use GridLayout.getChildCount()) : 要连接onClick,您可以遍历单元格(可以使用GridLayout.getChildCount()):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game_layout);
    gridLayout = (GridLayout) findViewById(R.id.gl_puzzle);
    . . . 

    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    for (int i = 0; i < NUM_CELLS; i++) {
        RelativeLayout tmpCell = (RelativeLayout) inflater.inflate(R.layout.picture_cell, gridLayout, false);
        ImageView pic = tmpCell.findViewById(R.id.iv_cell_image);
        pic.setImageBitmap(pieces.get(i));
        tmpCell.setOnClickListener(this);
        gridLayout.addView(tmpCell);
    }

where this is my xml layout file for picture_cell 这是我的picture_cell的xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_cell"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
   <ImageView
      android:id="@+id/iv_cell_image"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="fill"
      android:background="@android:color/background_dark"
      android:minHeight="20dp"
      android:minWidth="20dp"
      android:padding="1dp" />
</RelativeLayout>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM