获取自定义视图嵌入元素

问题描述 投票:0回答:2

我正在创建一个点击按钮的自定义视图。自定义视图是

<?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文本视图元素。

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

也许无论如何,通过ID来获取这个嵌入式视图其实并不是最好的方法?

先非常感谢您的帮助!

android android-view android-custom-view android-attributes
2个回答
0
投票

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

我想做的是访问嵌入在视图中的好友名称文本视图。 > 它的ID为"@id+friendName",但我似乎无法解决如何访问这个问题。 所以我想问题是--如果我知道FriendView元素的ID,那么我如何>获取其中的friendName文本视图元素。

你可以找到 friendName 查看 findViewById 在您创建的新的自定义视图实例上(您要添加到视图层次结构的那个)

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

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


0
投票

好吧,所以当我试图这样做的时候,我没有注意到一些东西,我把自己搞糊涂了。

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

findViewById方法是直接作用在View上的,所以我的问题是我没有在自定义视图类(FriendView)上进行findViewById。我用以下方法解决了这个问题。

this.findViewById(R.id.friendName);

从FriendView类中解决了这个问题。

回答我的后续问题--当我的自定义View创建时,我调试了代码,它将ID设置为-1。

© www.soinside.com 2019 - 2024. All rights reserved.