【问题标题】:Android Studio Custom View IssuesAndroid Studio 自定义视图问题
【发布时间】:2020-04-19 13:31:45
【问题描述】:

我急需帮助。几天来,我一直在努力解决我的问题,但我已经失去了继续我的项目的欲望。我似乎无法理解,所以请你帮我这样做。

我正在尝试创建一个包含好友列表的 activity_all_friends.xml。我的想法是我将拥有一个包含自定义视图(“friendView”)的线性布局。

我已经创建了朋友视图:

<?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>

无论如何,当我尝试在我的 activity_all_friends 布局上绘制这个新视图时,它显示为空白:

我还想通过点击右上角的“新建”按钮来添加这些新朋友。我已将 setOnClickListener 分配给按钮。按下按钮正在工作,新元素似乎已被删除,我只是看不到它们。

我的 FriendView 类:

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);
    }
}

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

另外2个小问题:布局文件夹里有我的friends_view.xml,对吗?属性从哪里来(attr.xml)?我想点击我的 API 并动态更改数字和朋友的名字。

感谢大家的帮助!!

【问题讨论】:

  • 为此使用自定义View 是相当不寻常的。您读过哪本有关 Android 应用程序开发的书教会了您这种方法?
  • 您可能还想尝试使用约束布局。
  • 仅当问题与 android studio IDE 本身有关时,请添加 android-studio 标签
  • @CommonsWare 其背后的想法是,每次创建“朋友”时,都会渲染整个friend_view 对象。每次创建“朋友”时,无需在代码中手动设置由friend_view 组成的所有视图。这不是正确的方法吗?你能提出一些不同的建议吗?我想重点是我每次都可以生成这个元素,而不是它包含的所有元素。
  • “这不是正确的方法吗?” ——正如我所写,这是相当不寻常的。我们通常为通用情况保留自定义视图,在这种情况下,我们需要一个呈现与我们可用的不同的小部件。特定领域场景(朋友、餐馆、发票)的自定义视图很难维护和测试。我们倾向于使用其他模式(例如,演示者),我们可以从渲染代码中提取业务逻辑,以获得更好的可测试性和灵活性。

标签: java android android-view android-custom-view


【解决方案1】:

您应该使用 inflater.inflate(R.layout.friend_view,this,true) 将您的布局膨胀到您的自定义视图中,而不分配给任何变量,您应该在您的 init 函数中调用它。

【讨论】:

  • 谢谢,你一针见血。我使用的 inflate 方法是错误的!
猜你喜欢
  • 2017-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-15
  • 2018-08-26
  • 1970-01-01
  • 2020-08-20
相关资源
最近更新 更多