获取自定义视图嵌入元素

时间:2020-04-22 20:06:33

标签: android android-view android-custom-view android-attributes

我要在单击按钮时创建一个自定义视图。自定义视图为:

<?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获取此嵌入式视图实际上并不是最好的方法吗?

非常感谢您的提前帮助!

2 个答案:

答案 0 :(得分:0)

用户在文本字段中输入一些文本(名称),然后按按钮创建? >新的自定义视图(他们的朋友列表中的朋友)。

我要执行的操作是访问嵌入在视图中的朋友名称文本视图。 >它的ID为“ @ id + / friendName”,但我似乎无法弄清楚如何访问它。 所以我想问题是-如果我知道FriendView元素的ID,那么我该如何>在其中获取friendName TextView元素。

您可以通过在您创建的自定义视图的新实例(您要添加到视图层次结构的视图)上使用friendName来找到findViewById视图。

还有一个问题,一旦创建另一个自定义视图(一个新朋友),每个自定义视图中嵌入视图的这些ID将相同,唯一的不同是自定义视图ID将有所不同。这些是在运行时随机生成的吗?

不。它们不是在运行时随机生成的。它们将是您已经分配给视图的内容(例如friendName)。

答案 1 :(得分:0)

好吧,所以当我尝试这样做时我并没有动过什么东西,而是让自己感到困惑。

我的自定义视图XML文件创建了ID,例如我感兴趣的TextView的“ @ id + / friendName”。这意味着已填充R.id.friendName。但是,我没有意识到让我感到困惑的更多是关于这个friendName ID出现的上下文。

findViewById方法直接作用于View,所以我的问题是在自定义视图类(FriendView)上找不到findViewById。我用以下方法解决了这个问题:

this.findViewById(R.id.friendName);

在FriendView类中。

要回答我的后续问题-创建我的customView并将其ID设置为-1时,我调试了代码。