【问题标题】:Why is Android Studio designer displaying my custom view nested inside itself, while it isn't为什么 Android Studio 设计器显示我的自定义视图嵌套在自身内部,而它不是
【发布时间】:2016-10-02 22:44:41
【问题描述】:

我的应用程序有一个名为 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 的超类,有一个小修复,没什么特别的。然后,我创建了一个使用我的自定义视图的视图:

<?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 的设计器视图中,我得到了这个:

编辑器显示我的视图层次结构,就像它有一个自身的嵌套实例一样,而显然没有。如果我删除任何一个,它们都会被删除。如果我在其中一个上声明属性,其他也会得到它。它们显然是同一个例子。唯一的例外是设置 ID。然后问题就消失了,只按预期显示了单个实例。

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

更新:不,现在,在编辑 id 之后,问题仍然存在。

更新 2: 这不仅仅是一个布局,所以我不能使用 &lt;include&gt; 标记。这是一个自定义视图,里面有自定义逻辑。

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

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 类的任何地方,而不仅仅是在一个地方。

【问题讨论】:

  • 绝对奇怪。除了布局 xml 文件中的视图之外,您的 Activity 类中是否有可能以编程方式添加视图?
  • @JoshuaCarmody 这正是我的想法。只有一个地方我膨胀了一个 xml,它位于刚刚从构造函数调用的初始化代码上(显然,一次),没有其他地方。第一个 XML 是我在我的问题中发布的那个。
  • 您是否尝试过与其他文件中的任何其他 CustomView 相同的操作?
  • @AkashAggarwal 是的,它也会再次发生。
  • 您可以尝试为每个视图指定一个 ID 或在另一台机器上测试相同的文件。也许问题出在android studio。

标签: android android-layout android-studio android-custom-view graphical-layout-editor


【解决方案1】:

在检查custom views documentation 之后,我没有找到任何理由在您的类构造函数方法中扩展相同的视图。尝试移除 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);
}

...

【讨论】:

  • 只是一个布局。这是一个带有自定义逻辑的自定义视图。
  • 你能在这里分享你的自定义视图类吗?
  • 你为什么要在 init 方法中再次膨胀视图?
  • 您认为我的视图代码(它只是一个类文件)会如何自动膨胀相应的 XML 并将其添加为自己的子文件?
  • 如果你还在膨胀布局,它总是两个视图。因为您在另一个(AvatarView)中膨胀了一个自定义视图(AdvancedRelativeLayout)。您是否尝试过动态构建它?
猜你喜欢
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
  • 2021-04-29
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
相关资源
最近更新 更多