gpt4 book ai didi

android - 为什么 Android Studio 设计器显示我的自定义 View 嵌套在自身内部,而它不是

转载 作者:太空狗 更新时间:2023-10-29 14:48:17 26 4
gpt4 key购买 nike

我的应用程序有一个自定义 View ,名为 AvatarView :

<?xml version="1.0" encoding="utf-8"?>
<com.ulouder.views.AdvancedRelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center_horizontal"
android:layout_margin="0dp"
android:padding="0dp"
xmlns:android="http://schemas.android.com/apk/res/android">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CP"
android:id="@+id/initialsView"
android:layout_alignTop="@+id/avatarView"
android:layout_alignLeft="@+id/avatarView"
android:layout_alignBottom="@+id/avatarView"
android:layout_alignRight="@+id/avatarView"
android:background="@drawable/avatar_background"
android:textColor="@color/white"
android:gravity="center"
android:textStyle="bold"
android:textSize="8sp" />

<com.makeramen.roundedimageview.RoundedImageView
app:riv_corner_radius="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/avatarView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="4dp"
app:riv_border_color="@color/lightGray"
app:riv_border_width="0.2dp" />

</com.uLouder.views.AdvancedRelativeLayout>

AdvancedRelativeLayout只是 RelativeLayout 的父类(super class)有一个小的修复,没有什么特别的。然后,我创建了一个使用自定义 View 的 View :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<com.ulouder.views.AvatarView
android:layout_width="50dp"
android:layout_height="50dp"/>

</LinearLayout>

也没什么好看的。但是在第二个布局 XML 的设计器 View 中,我得到了这个:

enter image description here

编辑器显示我的 View 层次结构,就像它具有自身的嵌套实例一样,但显然没有。如果我删除任何一个,它们都会被删除。如果我在其中一个上声明属性,其他人也会得到它。它们显然是同一个实例。唯一的异常(exception)是设置 ID。然后问题消失,如预期的那样只显示单个实例。

我已经重建了项目,重新启动了 Android Studio,但它仍然是一样的。我做错了什么?

更新: 不,现在,编辑后id ,问题依旧。

更新 2: 这不仅仅是一个布局,所以我不能使用 <include>标签。这是一个自定义 View ,其中包含自定义逻辑。

更新 3:这是我的自定义 View 的(相关)代码:

public class AvatarView extends FrameLayout {
public AvatarView(Context context) {
super(context);
init();
}

TextView initialsView;
RoundedImageView imageView;


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

void init(){
inflate(getContext(), R.layout.view_avatar, this);
initialsView = (TextView) findViewById(R.id.innerInitialsView);
imageView = (RoundedImageView) findViewById(R.id.innerImageView);
}

@SuppressWarnings("SuspiciousNameCombination")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec); //always square
imageView.setCornerRadius(widthMeasureSpec / 2f);
initialsView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, widthMeasureSpec * 30f);
}

}

更新 4: 似乎无论我放置自定义 AvatarView 的地方都会发生这种情况上课,而不仅仅是在一个地方。

最佳答案

在检查了 custom views documentation 之后,我没有找到任何理由在类构造函数方法中膨胀相同的 View 。 .尝试删除 init 方法中的 inflate。

...

public AvatarView(Context context) {
super(context);
init();
}

...

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

void init(){
// inflate(getContext(), R.layout.view_avatar, this);
initialsView = (TextView) findViewById(R.id.innerInitialsView);
imageView = (RoundedImageView) findViewById(R.id.innerImageView);
}

...

关于android - 为什么 Android Studio 设计器显示我的自定义 View 嵌套在自身内部,而它不是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37604681/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com