如何通过ImageView对象的名称获取属性的值?

时间:2018-06-19 14:14:09

标签: java android android-layout

我有3个3 x 3网格布局中的9个ImageView对象,这些对象在单击时都调用相同的函数。我需要一种方法来知道在调用函数时单击了网格中的哪个。 一种方法是使用getTag(),但我要使用值“ layout_row”和“ layout_column”属性。我该怎么办?

XML代码:

<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.v7.widget.GridLayout
    android:id="@+id/gridMain"
    android:layout_width="match_parent"
    android:layout_height="360dp"
    android:background="@drawable/board"
    app:columnCount="3"
    app:rowCount="3"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="153dp">

    <ImageView
        android:id="@+id/red9"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="0"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/red1"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:onClick="dropIn"
        app:layout_column="2"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/red7"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="2"
        app:layout_row="0" />

    <ImageView
        android:id="@+id/red3"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:onClick="dropIn"
        app:layout_column="0"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/red5"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="1"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/red4"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="2"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/red2"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="10dp"
        android:onClick="dropIn"
        app:layout_column="1"
        app:layout_row="2" />

    <ImageView
        android:id="@+id/red6"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="0"
        app:layout_row="1" />

    <ImageView
        android:id="@+id/red8"
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:layout_marginBottom="20dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="15dp"
        android:onClick="dropIn"
        app:layout_column="1"
        app:layout_row="0" />

我想要类似的东西:val = view.getAttributeValue("app","layout_row")

1 个答案:

答案 0 :(得分:0)

我认为GridView是执行这些操作的最佳选择。 顺便说一句,如果您要执行此操作,可以执行view.getId()并使用类似这样的开关进行比较:

switch(view.getId()){
    case R.id.red1:
        ...
        break;
    case R.id.red2:
    ...
}