代码之家  ›  专栏  ›  技术社区  ›  Elad Benda

当布局包含my customView的<merge>时,测试失败

  •  0
  • Elad Benda  · 技术社区  · 6 年前

      @Before
      public void initialize() {
        View view =
            LayoutInflater.from(RuntimeEnvironment.application)
                .inflate(R.layout.account_list_item, null);
        view.<AccountParticleDisc<Object>>findViewById(R.id.account_avatar)
            .initialize(imageLoader, Object.class);
        Mockito.reset(imageLoader); // Reset since initialize calls load the default image into the disc
        displayName = view.findViewById(R.id.account_display_name);
        accountName = view.findViewById(R.id.account_name);
        accountViewSetter = new AccountViewSetter<>(view, accountConverter);
      }
    

    用这个 account_list_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    
      <com.myView1
          android:id="@+id/account_avatar"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginStart="?attr/disc_margin_start"
          android:layout_marginLeft="?attr/disc_margin_start"
          android:padding="?attr/disc_padding"
          android:contentDescription="@null"
          app:imageViewSize="?attr/disc_imageViewSize"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          tools:src="@drawable/quantum_ic_account_circle_googblue_24"/>
      <android.support.constraint.Guideline
          android:id="@+id/account_display_name_top_guideline"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          app:layout_constraintGuide_begin="?attr/account_particle_guideline_vertical_offset"/>
      <TextView
          android:id="@+id/account_display_name"
          style="@style/AccountDataDisplayName"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="?attr/text_marginStart"
          android:layout_marginEnd="?attr/text_marginEnd"
          android:layout_marginLeft="?attr/text_marginStart"
          android:layout_marginRight="?attr/text_marginEnd"
          android:gravity="center_vertical"
          android:includeFontPadding="false"
          app:layout_constraintBottom_toTopOf="@+id/account_name"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0"
          app:layout_constraintStart_toEndOf="@+id/account_avatar"
          app:layout_constraintTop_toBottomOf="@id/account_display_name_top_guideline"
          app:layout_constraintVertical_chainStyle="packed"
          app:layout_constraintWidth_default="wrap"
          tools:text="DisplayNameIssLongSoItShouldBeTruncatedAtSomePoint"/>
      <TextView
          android:id="@+id/account_name"
          style="@style/AccountDataAccountName"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_marginStart="?attr/text_marginStart"
          android:layout_marginEnd="?attr/text_marginEnd"
          android:layout_marginLeft="?attr/text_marginStart"
          android:layout_marginRight="?attr/text_marginEnd"
          android:gravity="center_vertical"
          android:includeFontPadding="false"
          app:layout_constraintBottom_toTopOf="@+id/account_name_bottom_guideline"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0"
          app:layout_constraintStart_toEndOf="@+id/account_avatar"
          app:layout_constraintTop_toBottomOf="@+id/account_display_name"
          app:layout_constraintWidth_default="wrap"
          tools:text="emailisverylongaswellwewantittogettruncated@gmail.longdomain.com"/>
    
      <android.support.constraint.Guideline
          android:id="@+id/account_name_bottom_guideline"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:orientation="horizontal"
          app:layout_constraintGuide_end="?attr/account_particle_guideline_vertical_offset"/>
    
    
    </android.support.constraint.ConstraintLayout>
    

    但是当我把这个重构成这个 account_list_it.xml 布局。

    <?xml version="1.0" encoding="utf-8"?>
    <com.MyView2
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/has_selected_account"
        android:layout_width="match_parent"
        android:layout_height="@dimen/account_particle_account_list_item_height"
        app:apStyle="list_item"
        app:layout_constraintHorizontal_bias="0"/>
    

    account\u list\u it.xml 布局

    <merge
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools">
    
    ..
    
    </merge>
    

    但根布局是自定义各个视图的祖先:

    公共类MyView2扩展了ConstraintLayout{

    Binary XML file line #2: Binary XML file line #2: Error inflating class <unknown>
    
    android.view.InflateException: Binary XML file line #7: Failed to resolve attribute at index 3: TypedValue{t=0x2/d=0x7f010073 a=-1}
    

    android.view.InflateException: 
    Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class <unknown>
    

    1 回复  |  直到 6 年前
        1
  •  0
  •   Aaron    6 年前

    每当使用展开视图时,必须提供父视图 merge 标签:

    Context context = RuntimeEnvironment.application;
    View parent = new FrameLayout(context);
    View view = LayoutInflater.from(context).inflate(R.layout.account_list_item, parent, true);