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

时间:2018-07-09 13:08:16

标签: java android xml android-layout android-studio

因此我目前正在处理自定义卡片视图,我的目标非常简单。我想将文本字段更新为在自定义卡片视图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文件中设置的属性值...

但是一旦加载,编辑器视图就会崩溃...由于Custom CardView类...没有真正的错误日志...

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

我忘记了什么?我还能尝试什么修改编辑器的textview?

1 个答案:

答案 0 :(得分:0)

这很容易...只是添加了一个新的布局活动,该活动将在按下CardView后膨胀。我只是复制CardView元素并将其放置在膨胀视图内=>问题已解决:)