代码之家  ›  专栏  ›  技术社区  ›  genaray

使自定义视图在编辑器中更新文本字段

  •  2
  • genaray  · 技术社区  · 7 年前

    所以我目前正在开发一个定制的卡片视图,我的目标很简单。我想将textfield更新为我在自定义卡视图xml中设置的字符串。

    但不知怎的,经过几个小时的努力,我还是无法让它工作。有时编辑器不会用自定义卡视图加载XML…有时它会给我一个非常长的错误信息…有时它只是保持黑色…

    因此,我有以下XML文件:

    <LinearLayout 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:id="@+id/CallEntry_Holder"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:tag="0">
    
    <com.test.larsmatthaus.projectnurser.customs.Custom_CardView
    
        android:id="@+id/Call_Entry"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        app:cardBackgroundColor="@color/colorCardBackgroundR"
        app:cardCornerRadius="5dp"
        app:personName="test"
        app:room="100"
        app:ward="test"
        tools:layout_editor_absoluteX="1024dp">
    
        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="50dp">
    
            <TextView
                android:id="@+id/WardName"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_marginBottom="10dp"
                android:layout_marginEnd="200dp"
                android:layout_marginLeft="200dp"
                android:layout_marginRight="200dp"
                android:layout_marginStart="200dp"
                android:layout_marginTop="10dp"
                android:autoSizeTextType="uniform"
                android:fontFamily="@font/comfortaa_light"
                android:gravity="center"
                android:text="Louisa Burgess"
                android:textColor="@color/colorOverlayTC"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <ImageView
                android:id="@+id/icon_2"
                android:layout_width="31dp"
                android:layout_height="0dp"
                android:layout_marginBottom="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/icon_1"
                app:layout_constraintHorizontal_bias="0.85"
                app:layout_constraintStart_toEndOf="@+id/WardName"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/teddybear" />
    
            <ImageView
                android:id="@+id/icon_1"
                android:layout_width="31dp"
                android:layout_height="0dp"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="8dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="8dp"
                android:adjustViewBounds="true"
                android:scaleType="fitCenter"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.95"
                app:layout_constraintStart_toEndOf="@+id/WardName"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/bed" />
    
            <TextView
                android:id="@+id/RoomText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="8dp"
                android:layout_marginStart="16dp"
                android:layout_marginTop="8dp"
                android:autoSizeTextType="uniform"
                android:fontFamily="@font/comfortaa_light"
                android:gravity="center"
                android:text="105"
                android:textColor="@color/colorOverlayTC"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/WardName"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                android:id="@+id/seperator_C"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:fontFamily="@font/comfortaa_light"
                android:text="/"
                android:textColor="@color/colorOverlayTC"
                android:textSize="24sp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="@+id/RoomText"
                app:layout_constraintEnd_toStartOf="@+id/WardName"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/RoomText"
                app:layout_constraintTop_toTopOf="@+id/RoomText" />
    
            <TextView
                android:id="@+id/WardText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:fontFamily="@font/anaheim"
                android:gravity="center"
                android:text="Ward 2"
                android:textColor="@color/colorOverlayTC"
                android:textSize="18sp"
                app:layout_constraintBottom_toBottomOf="@+id/seperator_C"
                app:layout_constraintEnd_toStartOf="@+id/WardName"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintStart_toEndOf="@+id/seperator_C"
                app:layout_constraintTop_toTopOf="@+id/seperator_C" />
    
        </android.support.constraint.ConstraintLayout>
    
    </com.test.larsmatthaus.projectnurser.customs.Custom_CardView>
    

    它只包含一个卡视图条目。

    我的自定义卡视图类如下所示:

    public class Custom_CardView{
    
    
    public Context context;
    
    public Map<Integer, Object> vars = new HashMap<Integer, Object>();
    
    
    public Custom_CardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    
    
        this.context = context;
    
        setWillNotDraw(false);
    
    
        View view = getRootView();
    
    
        for(int index = 0; index<((ViewGroup)view).getChildCount(); ++index) {
    
            View nextChild = ((ViewGroup)view).getChildAt(index);
    
            vars.put(nextChild.getId(), nextChild);
    
        }
    
        Log.println(Log.DEBUG,"Custom_CardView","<Custom_CardView : Instantiated => ["+vars.size()+"] children>");
    
    
    
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
        view = inflater.inflate(R.layout.elements_call_entry, this, true);
    
    
        TextView wardText = (TextView) findViewById(R.id.WardName);
        wardText.setText("TEST");
    
    
    }
    
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
    
    
    }
    
    }
    

    我想将textview“wardname”更新为我在XML文件中设置的属性值…

    但不知怎么的,一旦我加载编辑器视图,它就会崩溃…由于自定义cardview类…没有真正的错误日志…

    我唯一收到的是: 呈现布局时预览超时。当自定义视图中存在无限循环或无限递归时,通常会发生这种情况。

    我忘了什么?我还可以尝试修改编辑器的文本视图吗?

    1 回复  |  直到 7 年前
        1
  •  1
  •   genaray    7 年前

    很简单…刚添加了一个新的布局活动,一旦按下cardview就会膨胀。我只需复制cardView元素并将其放入充气视图中=>问题已解决:)