You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中BottomSheet的ImageButton与ImageView不显示求助

待办事项App BottomSheet中ImageButton与ImageView无法显示求助

在我的待办事项App项目中,我尝试创建一个BottomSheet,但运行后发现其中的ImageButton和ImageView无法正常显示。以下是对应的布局XML文件、Java代码以及运行效果截图,我已在网络上查找类似问题但未找到有效解决方案,特此求助。

布局XML代码

<?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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/rouned_corners_dialog"
    android:padding="10dp">

    <ImageView
        android:id="@+id/imageView5"
        android:layout_width="373dp"
        android:layout_height="14dp"
        android:layout_marginBottom="10dp"
        android:contentDescription="@string/arrowbtn_bottomsheet"
        app:srcCompat="@drawable/arrow1" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:contentDescription="@string/newtask_text"
        app:srcCompat="@drawable/create_new_task_txt" />

    <EditText
        android:id="@+id/task_name"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_marginVertical="10dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="@string/task_name"
        android:textSize="13sp"
        android:textColor="#757575" />

    <EditText
        android:id="@+id/task_location"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:layout_marginBottom="10dp"
        android:ems="10"
        android:inputType="textPersonName"
        android:text="@string/Location"
        android:textSize="13sp"
        android:textColor="#757575" />


    <EditText
        android:id="@+id/task_date"
        android:layout_width="250dp"
        android:layout_height="50dp"
        android:ems="10"
        android:inputType="date"
        android:text="@string/mm_dd_year"
        android:textSize="13sp"
        android:textColor="#757575"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="381dp"
        android:layout_height="79dp"
        android:layout_marginTop="10dp">


        <ImageButton
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:contentDescription="@string/cancel_btn_bottomsheet"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.068"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/cancel_btn" />

        <ImageButton
            android:id="@+id/add_task_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@null"
            android:contentDescription="@string/add_task_btn_bottomsheet"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.931"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@drawable/addtask_btn" />

    </androidx.constraintlayout.widget.ConstraintLayout>

</LinearLayout>

Java代码

ImageButton add_list_button1=findViewById(R.id.addlistbtn);
add_list_button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        add_list_bottomsheet=new BottomSheetDialog(MainActivity.this, R.style.bottom_sheet_theme);
        View sheetview= LayoutInflater.from(getApplicationContext()).inflate(R.layout.add_list_bottomsheet, null);

        sheetview.findViewById(R.id.cancel_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                add_list_bottomsheet.dismiss();
            }
        });

        add_list_bottomsheet.setContentView(sheetview);
        add_list_bottomsheet.show();

    }
});

运行效果截图

运行效果截图


解决方案

1. 修复布局加载上下文问题

你使用getApplicationContext()加载布局,这会缺失Activity主题的资源配置,而app:srcCompat依赖AppCompat主题的支持。替换成当前Activity上下文:

View sheetview= LayoutInflater.from(MainActivity.this).inflate(R.layout.add_list_bottomsheet, null);

2. 确保Vector Drawable支持配置正确

如果你的图片是Vector格式,在Module级build.gradle中添加配置,保证AppCompat能正确加载:

android {
    defaultConfig {
        vectorDrawables.useSupportLibrary = true
    }
}

3. 检查资源文件有效性

确认@drawable/arrow1、@drawable/create_new_task_txt等资源文件存在于res/drawable目录下,且文件名全小写(Android资源文件名不允许大写)。

4. 替换固定宽度为自适应

布局中设置的固定宽度(373dp、381dp)会导致不同分辨率设备显示异常,建议改为自适应:

<ImageView
    android:id="@+id/imageView5"
    android:layout_width="match_parent"
    android:layout_height="14dp"
    android:layout_marginBottom="10dp"
    android:contentDescription="@string/arrowbtn_bottomsheet"
    android:scaleType="fitCenter"
    app:srcCompat="@drawable/arrow1" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="79dp"
    android:layout_marginTop="10dp">

5. 检查BottomSheet主题配置

确保bottom_sheet_theme继承自AppCompat主题,无冲突属性:

<style name="bottom_sheet_theme" parent="Theme.AppCompat.Light.Dialog">
    <item name="bottomSheetStyle">@style/AppBottomSheetDialog</item>
</style>

<style name="AppBottomSheetDialog" parent="Widget.Design.BottomSheet.Modal">
    <item name="android:background">@drawable/rouned_corners_dialog</item>
</style>

优先尝试修改上下文和资源配置,这是此类问题的常见成因。


内容的提问来源于stack exchange,提问作者jedddi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 14:17:14