0

ボタンをクリックするだけでカスタムビューを作成しています。カスタム ビューは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:gravity="center"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/friendImage"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:contentDescription="Person Image"
        app:srcCompat="@drawable/person2" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/friendName"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name of friend"
            android:textAlignment="center"
            android:textColor="#000000"
            android:textSize="20sp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionInPersonIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/in_person" />

                <TextView
                    android:id="@+id/interactionInPersonText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionVideoIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_margin="3dp"
                    android:layout_weight="1"
                    android:tint="#292828"
                    app:srcCompat="@android:drawable/presence_video_online" />

                <TextView
                    android:id="@+id/interactionVideoText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionTextIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    app:srcCompat="@drawable/text_icon" />

                <TextView
                    android:id="@+id/interactionTextText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/interactionPhoneIcon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:tint="#353535"
                    app:srcCompat="@drawable/phone_icon" />

                <TextView
                    android:id="@+id/interactionPhoneText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="299"
                    android:textAlignment="center" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

ユーザーはテキスト (名前) をテキスト フィールドに入力し、ボタンを押して新しいカスタム ビュー (フレンド リスト内のフレンド) を作成します。

私がやろうとしているのは、ビューに埋め込まれている友人名のテキスト ビューにアクセスすることです。IDは「@id+/friendName」ですが、これにアクセスする方法がわかりません。

したがって、問題は、FriendView 要素の ID がわかっている場合、その中の friendName TextView 要素を取得するにはどうすればよいかということだと思います。

また、別のカスタム ビューが作成されると (新しいフレンド)、各カスタム ビュー内に埋め込まれたビューのこれらの ID が同じになるという問題もあります。唯一の違いは、カスタム ビュー ID が異なることです。これらは実行時にランダムに生成されますか?

とにかく、この埋め込みビューを ID で取得することは、実際には最善の方法ではないのでしょうか?

事前にご協力いただきありがとうございます。

4

2 に答える 2

0

ユーザーはテキスト (名前) をテキスト フィールドに入力し、ボタンを押して ? > 新しいカスタム ビュー (フレンド リスト内のフレンド)。

私がやろうとしているのは、ビューに埋め込まれている友人名のテキスト ビューにアクセスすることです。> ID は「@id+/friendName」ですが、これにアクセスする方法がわかりません。したがって、問題は、FriendView 要素の ID がわかっている場合、その中の friendName TextView 要素を取得するにはどうすればよいかということだと思います。

自分で作成したカスタム ビューの新しいインスタンス (ビュー階層に追加するもの) をfriendName使用して、ビューを見つけることができます。findViewById

また、別のカスタム ビューが作成されると (新しいフレンド)、各カスタム ビュー内に埋め込まれたビューのこれらの ID が同じになるという問題もあります。唯一の違いは、カスタム ビュー ID が異なることです。これらは実行時にランダムに生成されますか?

いいえ、実行時にランダムに生成されるわけではありません。それらは、ビューに既に割り当てられているものになります (例: friendName)。

于 2020-04-22T20:45:55.030 に答える