java - 如何将自定义 View 与其他 UI 组件一起使用?

标签 java android android-linearlayout

我的问题是我想得到这样的结果:

这是一个插图

enter image description here

所以我需要顶部有一个工具栏,然后是一个自定义 View (我使用 Canvas 让用户绘制对象 - 圆圈和圆圈之间的边缘),最后是底部的布局(带有 ImageButton 组件 - 用于清除自定义 View 中 Canvas 的示例)。

一切工作正常,除了我无法添加底层(使用 ImageButtons),因为自定义 View 占用了所有可用空间。

我的代码:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/bogoToolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#27A9E0"
        android:minHeight="45dp"
        app:titleTextColor="@android:color/white">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Breadth-First Search Visualization"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold"
            app:fontFamily="@font/roboto_slab" />

    </androidx.appcompat.widget.Toolbar>

    <com.globalsoftwaresupport.graph.bfs.BreadthFirstSearchView
        android:id="@+id/breadthFirstSearchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="68dp"
        android:minHeight="100dp"
        android:orientation="horizontal">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quiz_icon" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/quiz_icon2" />

    </LinearLayout>

</LinearLayout>

你对我做错了什么有什么建议吗?提前致谢

最佳答案

您最好使用 ConstraintLayout。将工具栏约束到顶部,将带有按钮的 Linearlayout 约束到底部,然后将 BreadthFirstSearchView 顶部约束到工具栏底部,边距为 0dp,BreadthFirstSearchView 底部约束到 LinearLayout 顶部,边距为 0dp,并将 BreadthFirstSearchView 的layout_height 设置为MATCH_CONSTRAINTS(0dp)。您的布局文件应如下所示:

<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">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/bogoToolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#27A9E0"
    android:minHeight="45dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:titleTextColor="@android:color/white">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Breadth-First Search Visualization"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:textStyle="bold"
        app:fontFamily="@font/roboto_slab" />

</androidx.appcompat.widget.Toolbar>

<com.globalsoftwaresupport.graph.bfs.BreadthFirstSearchView
    android:id="@+id/breadthFirstSearchView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="0dp"
    android:layout_marginBottom="0dp"
    app:layout_constraintBottom_toTopOf="@+id/linearLayout"
    app:layout_constraintTop_toBottomOf="@+id/bogoToolbar" />

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="68dp"
    android:minHeight="100dp"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/quiz_icon" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/quiz_icon2" />

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

最后一件事。如果您希望 View 显示在 Android Studio 的编辑器中并能够从 XML 扩充它。您必须提供此构造函数:

public YourView(Context context, AttributeSet attrs) {
        super(context, attrs);

}

祝你好运!

关于java - 如何将自定义 View 与其他 UI 组件一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58799721/

相关文章:

java - Android CircularReveal 错误 : Not working after starting another type of animator on the same view

android - 对话框中的背景颜色显示不正确

android - ScrollView 根本不滚动

Android:LinearLayout 的布局问题

Java 2d 数组与图 block 的坐标错误

java - JVM 抛出 java.lang.OutOfMemoryError : heap space (File processing)

java - 在我的项目中组织读/写操作的更好方法

java - 将长日期转换为我指定日期的日期时间格式

android - 为什么我在代码中运行 TextView.measure() 时得到的结果与我在屏幕上看到的呈现不同

android - 我的广告未显示,广告加载失败 : 3