0

Java 的菜鸟,所以我的知识有限。我有 2 个相对布局,它们将保存 13 张牌手的图像。一张代表所有牌,一张略微偏移,显示可以打出哪些牌。Xml 相当简单

           <!--Player viewable cards-->
    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="200dp"
        android:layout_height="680dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="15dp"
        android:id="@+id/rlUserViewCards">

        <ImageView
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:src="@drawable/c001"
            android:rotation="90"
            android:tag="1"
            android:id="@+id/card1"
            android:clickable="false">
        </ImageView>

        <ImageView
            android:layout_width="100dp"
            android:layout_height="85dp"
            android:src="@drawable/c002"
            android:rotation="90"
            android:id="@+id/card2"
            android:layout_below="@+id/card1"
            android:layout_marginTop="-65dp"
            android:clickable="false">
        </ImageView>

依此类推,共 13 张图像。然后,我可以创建一个相对布局的视图,该视图通过孩子们循环制作图像不可见/可见,可点击或不可点击。这看起来是个好方法吗?

如果是这样,我在第一次设置 relativelayout 时的问题是我可以在上面做以及用卡片值设置 Tag,我不知道如何通过 findViewById 获取 ImageView 的整数。

            userViewCards = (RelativeLayout) findViewById(R.id.rlUserViewCards);
    userPickCards = (RelativeLayout) findViewById(R.id.rlUserPlayCards);
    View v = userViewCards.getChildAt(0);
    Resources res = getResources();
    int resId = res.getIdentifier("s204", "drawable", getPackageName());  
    Drawable drawable = ResourcesCompat.getDrawable(getResources(), res.getIdentifier("s204", "drawable", getPackageName()), null);
    ImageView insertImage = (ImageView) findViewById(??);  
    insertImage.setImageDrawable(drawable);

谁能指出我正确的方向?

谢谢!

4

1 回答 1

0

您可以为 imageView 设置标签值并在您的活动中接收它,如下所示

<ImageView
        android:layout_width="100dp"
        android:layout_height="85dp"
        android:src="@drawable/c001"
        android:rotation="90"
        android:tag="1"
        android:id="@+id/card1"
        android:clickable="false">


@OnClick({R.id.card1, R.id.card2, R.id.card3, R.id.card4})
public void selectedCard(View view) {

    String selectedCard = view.getTag().toString();
    Log.d(TAG, "Mode Selected : " + mode);

    switch (Integer.parseInt(mode)) {
        case Constant.Card1:
            // Do something here
            break;
        case Constant.Card2:
            // Do something here
            break;
        case Constant.Card3:
            // Do something here
            break;
        case Constant.Card4:
            // Do something here
    }
}

注意:我在这里使用黄油刀,因此 @OnClick()

于 2016-08-28T19:45:53.040 回答