Java Android Studio自定义视图问题

Java Android Studio自定义视图问题,java,android,android-view,android-custom-view,Java,Android,Android View,Android Custom View,我急需帮助。我已经尝试解决我的问题好几天了,我失去了继续我的项目的任何愿望。我似乎不明白,所以请你帮我理解一下 我正在尝试创建一个包含朋友列表的activity\u all\u friends.xml。我的想法是,我会有一个线性布局,容纳自定义视图(“friendView”) 我创建了friendView: 有人知道为什么会这样吗?我的理解是,我需要编写我想要生成的xml元素(在我的例子中是friend_view.xml),我想要编写动态处理元素发生的事情的类(在我的例子中是friendVie

我急需帮助。我已经尝试解决我的问题好几天了,我失去了继续我的项目的任何愿望。我似乎不明白,所以请你帮我理解一下

我正在尝试创建一个包含朋友列表的activity\u all\u friends.xml。我的想法是,我会有一个线性布局,容纳自定义视图(“friendView”)

我创建了friendView:

有人知道为什么会这样吗?我的理解是,我需要编写我想要生成的xml元素(在我的例子中是friend_view.xml),我想要编写动态处理元素发生的事情的类(在我的例子中是friendView类),然后我想要将它链接到我想要生成它的布局(在我的例子中是activity_all_friends.xml)。这是正确的吗

另外两个小问题:我在布局文件夹中有我的friends_view.xml,是否正确?属性来自哪里(attr.xml)?我想点击我的API并动态更改号码和朋友的名字


谢谢你的帮助

您应该使用充气器将布局充气到您的自定义视图中。充气(R.layout.friend\u view,this,true)不指定任何变量,您应该在init函数中调用它。

使用自定义
视图
这是非常不寻常的。你读过哪本关于Android应用程序开发的书教你这种方法?你可能还想尝试使用Constraint Layout。只有当问题与Android studio IDE相关时,请添加
Android studio
标记itself@CommonsWare其背后的思想是,每一次创造一个"朋友",,整个friend_视图对象将被渲染。而不是在每次创建“朋友”时在代码中手动设置“朋友”视图所包含的所有视图。这不是正确的方法吗?你能提出不同的建议吗?我想关键是我每次都可以生成一个元素,而不是它包含的所有元素。“这不是正确的方法吗?”——正如我所写的,这是非常不寻常的。我们通常为通用情况保留自定义视图,在这种情况下,我们需要一个呈现方式不同于现有情况的小部件。特定领域场景(朋友、餐厅、发票)的自定义视图很难维护和测试。我们倾向于使用其他模式(例如Presenter),从呈现代码中提取业务逻辑,以获得更好的可测试性和灵活性。谢谢,你一针见血。我使用的充气方法是错误的!
<?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_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_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" />
            </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>
public class FriendView extends LinearLayout {
    private Context context;
    private String friend;

    public FriendView(Context context) {
        super(context);
        init(null,0);
    }

    public FriendView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(attrs,0);
    }

    public FriendView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs,defStyle);
        init(attrs,0);
    }

    private void init(AttributeSet attrs, int defStyle){

    }

    public String getFriend() {
        return friend;
    }

    public void setFriend(String friend) {
        this.friend = friend;
    }

    public View getView(View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.friend_view, null);

        TextView friendName = (TextView) view.findViewById(R.id.friendView);
        TextView interactionInPersonText = (TextView) view.findViewById(R.id.interactionInPersonText);
        TextView interactionVideoText = (TextView) view.findViewById(R.id.interactionVideoText);
        TextView interactionTextText = (TextView) view.findViewById(R.id.interactionTextText);
        TextView interactionPhoneText = (TextView) view.findViewById(R.id.interactionPhoneText);

        // TODO
        friendName.setText("Temp random");
        interactionInPersonText.setText(Long.toString(Math.round(Math.random() * 300)));
        interactionVideoText.setText(Long.toString(Math.round(Math.random() * 300)));
        interactionTextText.setText(Long.toString(Math.round(Math.random() * 300)));
        interactionPhoneText.setText(Long.toString(Math.round(Math.random() * 300)));

        return view;
    }

    @Override
    protected void onDraw(Canvas canvas){
        canvas.drawColor(Color.BLUE);
    }
}