Android 中的 GridLayout 不显示图像

GridLayout not showing images in Android

所以这是 xml 文件,在 RelativeLayout 中有 GridLayout

<?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="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorSecondary" />

    <GridLayout
        android:id="@+id/grid_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/main_toolbar"
        android:layout_marginTop="50dp"
        android:orientation="horizontal"
        android:rowCount="1"
        android:useDefaultMargins="true">

        <ImageView
            android:id="@+id/dal_bati"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:onClick="buyDalBati"
            android:src="@drawable/dal_bati" />

        <ImageView
            android:id="@+id/gulab_jamun"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:onClick="buyGulabJamun"
            android:src="@drawable/gulab_jamun" />

        <ImageView
            android:id="@+id/gajar_halwa"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_columnWeight="1"
            android:onClick="buyGajarHalwa"
            android:src="@drawable/gajar_halwa" />
    </GridLayout>
</RelativeLayout>

我试过图像格式 jpeg、jpg 和 png 没有图像格式显示图像。

包含图像的可绘制文件夹

我在每张图片上都设置了 onclick 侦听器,所以如果我假设它是这里的图片,那么它将打开下一个 activity 购买它意味着 onClickListener 正在工作,只有图片没有显示.

MainActivity代码

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.main_toolbar);

        //setting the toolbar
        setSupportActionBar(toolbar);
        if(getSupportActionBar() != null) {
            getSupportActionBar().setTitle("Click To Buy");
        }
    }

    public void buyGajarHalwa(View view) {
        Intent intent = new Intent(this,GajarHalwa.class);
        startActivity(intent);
    }

    public void buyGulabJamun(View view) {
        Intent intent = new Intent(this,GulabJamun.class);
        startActivity(intent);
    }

    public void buyDalBati(View view) {
        Intent intent = new Intent(this,DalBati.class);
        startActivity(intent);
    }
}

这是主题代码


<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.GridLayoutExperiment" parent="Theme.MaterialComponents.DayNight.NoActionBar">
        <!-- Primary brand color. -->
        <item name="colorPrimary">@color/purple_500</item>
        <item name="colorPrimaryVariant">@color/purple_700</item>
        <item name="colorOnPrimary">@color/white</item>
        <!-- Secondary brand color. -->
        <item name="colorSecondary">@color/teal_200</item>
        <item name="colorSecondaryVariant">@color/teal_700</item>
        <item name="colorOnSecondary">@color/black</item>
        <!-- Status bar color. -->
        <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
        <!-- Customize your theme here. -->
    </style>
</resources>

这是因为你给的是 android:width="wrap_content" ,它作为图片的实际内容。所以,请将它设置为 0dp,如下所示:并且,还要提供固定的高度,否则它会占用整个屏幕。而且,我给每个 imageview 都留了边距,这样看起来不错。

<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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<androidx.appcompat.widget.Toolbar
    android:id="@+id/main_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorSecondary" />

<GridLayout
    android:id="@+id/grid_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/main_toolbar"
    android:layout_marginTop="50dp"
    android:orientation="horizontal"
    android:rowCount="1"
    android:useDefaultMargins="true">

    <ImageView
        android:id="@+id/dal_bati"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_columnWeight="1"
        android:onClick="buyDalBati"
        android:layout_margin="5dp"
        android:src="@drawable/dal_bati"
        />

    <ImageView
        android:id="@+id/gulab_jamun"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_columnWeight="1"
        android:onClick="buyGulabJamun"
        android:layout_margin="5dp"
        android:src="@drawable/gulab_jamun"
        />

    <ImageView
        android:id="@+id/gajar_halwa"
        android:layout_width="0dp"
        android:layout_height="100dp"
        android:layout_columnWeight="1"
        android:layout_margin="5dp"
        android:onClick="buyGajarHalwa"
        android:src="@drawable/gajar_halwa"
        />
   </GridLayout>
 </RelativeLayout>