我想制作一个带边框的网格,所以我创建了一个空单元格视图,它是较大的黑色视图之上的较小的白色视图,因此它看起来像是一个带有黑色边框的白色单元格,但是当我将其放入网格中时,它什么也没有显示,即使我指定宽度和高度它仍然不显示任何内容
空_cell.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:layout_gravity="center"
tools:ignore="MissingDefaultResource">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:foregroundGravity="center"
android:background="#bbb">
<View
android:id="@+id/frame"
android:layout_width="100sp"
android:layout_height="25sp"
android:background="@color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:layout_width="96sp"
android:layout_height="21sp"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="@+id/frame"
app:layout_constraintEnd_toEndOf="@+id/frame"
app:layout_constraintStart_toStartOf="@+id/frame"
app:layout_constraintTop_toTopOf="@+id/frame" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
EmptyCell.java:
package com.example.teacherplanner;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import androidx.annotation.Nullable;
public class EmptyCell extends RelativeLayout {
public EmptyCell(Context context) {
super(context);
init(context);
}
public EmptyCell(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.empty_cell, this, true);
}
}
我想在其中使用自定义视图的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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="match_parent"
android:layout_height="match_parent"
tools:context=".WeekEditing"
>
<GridLayout
android:id="@+id/LOOK_AT_WEEK_HERE"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintHeight_percent="0.7"
android:gravity="center"
tools:ignore="MissingConstraints,SpeakableTextPresentCheck"
android:orientation="horizontal">
<!--**************** CUSTOM VIEW *****************-->
<com.example.teacherplanner.EmptyCell
android:layout_width="100sp"
android:layout_height="25sp"/>
<!--**********************************************-->
</GridLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/edit_button_window"
android:layout_width="match_parent"
android:layout_height="0sp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.3"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:id="@+id/edit_subject_buttons"
android:layout_width="match_parent"
android:layout_height="40sp"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/EDIT_BUTTON"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/blue_button_on"
android:textColor="@color/button_text_on"
android:text="EDIT"
android:textSize="25sp"
tools:ignore="HardcodedText,TouchTargetSizeCheck,VisualLintButtonSize"
/>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/SUBJECT_BUTTON"
android:layout_width="0sp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/blue_button_off"
android:textColor="@color/button_text_off"
android:text="SUBJECT"
android:textSize="25sp"
tools:ignore="HardcodedText,TouchTargetSizeCheck,VisualLintButtonSize" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/EDIT_WINDOW"
android:layout_width="match_parent"
android:layout_height="0sp"
android:orientation="horizontal"
android:background="@color/blue_button_on"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_subject_buttons">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/CLEAR_WEEK_BUTTON"
android:layout_width="100sp"
android:layout_height="match_parent"
android:background="#f11"
android:text="CLEAR WEEK"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="HardcodedText" />
<!-- FROM/TO SECTION -->
<TextView
android:id="@+id/from_date"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_marginStart="16sp"
android:layout_marginTop="16sp"
android:gravity="center"
android:text="From: "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="HardcodedText" />
<androidx.cardview.widget.CardView
android:id="@+id/from_card"
android:layout_width="80sp"
android:layout_height="30sp"
android:background="#a54"
app:layout_constraintLeft_toRightOf="@+id/from_date"
app:layout_constraintTop_toTopOf="@+id/from_date" />
<TextView
android:id="@+id/to_date"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_marginStart="16sp"
android:gravity="center"
android:text="To: "
app:layout_constraintStart_toEndOf="@+id/from_card"
app:layout_constraintTop_toTopOf="@+id/from_card"
tools:ignore="HardcodedText" />
<androidx.cardview.widget.CardView
android:id="@+id/to_card"
android:layout_width="80sp"
android:layout_height="30sp"
android:background="#a54"
app:layout_constraintLeft_toRightOf="@+id/to_date"
app:layout_constraintTop_toTopOf="@+id/to_date" />
<!-- INTERVALS -->
<TextView
android:id="@+id/intervals"
android:layout_width="wrap_content"
android:layout_height="30sp"
android:layout_marginStart="16sp"
android:layout_marginTop="16sp"
android:text="Intervals: "
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/from_date"
tools:ignore="HardcodedText" />
<androidx.cardview.widget.CardView
android:layout_width="100sp"
android:layout_height="30sp"
app:layout_constraintStart_toEndOf="@+id/intervals"
app:layout_constraintTop_toTopOf="@+id/intervals" />
</androidx.constraintlayout.widget.ConstraintLayout>
<ScrollView
android:id="@+id/SUBJECT_WINDOW"
android:layout_width="match_parent"
android:layout_height="0sp"
android:orientation="horizontal"
android:visibility="invisible"
android:background="@color/blue_button_on"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_subject_buttons"
tools:ignore="SpeakableTextPresentCheck">
<GridLayout
android:id="@+id/SUBJECT_GRID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="3"
android:gravity="center"
android:padding="10sp"
>
<ImageButton
android:id="@+id/add_subject_btn"
android:layout_width="50sp"
android:layout_height="50sp"
android:layout_margin="10sp"
android:backgroundTint="#00FFFFFF"
android:src="@drawable/plus_circle"
app:layout_constraintDimensionRatio="1:1"
tools:ignore="SpeakableTextPresentCheck" />
</GridLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试将其放入网格视图中,采用线性布局,但它仍然只显示一个空白空间
事实证明,即使我在运行模拟器后在设计布局上看不到它,它也能正常工作