【问题标题】:Not getting images in imageButtons at all根本没有在 imageButtons 中获取图像
【发布时间】:2016-09-25 15:28:18
【问题描述】:

在下面的布局文件中,对于最后一个线性布局,我添加了三个带有有效图像和文本的图像按钮。但是,它们根本不会显示在应用程序中。下面给出了这个的布局文件。

  <?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:layout_width="match_parent"
    android:layout_height="120dp"

    android:background="?android:attr/selectableItemBackground"
    android:clickable="true"
    android:focusable="true"
    android:orientation="vertical">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/female_default"
            android:layout_gravity="center_horizontal"
            android:id="@+id/imageView2"
            android:layout_weight="1" />

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <TextView
                android:text="TextView"
                android:layout_gravity="center_horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/textView3" />

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:text="TextView"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/textView2" />

                <TextView
                    android:id="@+id/id"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/text_margin"
                    android:text="Balle"
                    android:textAppearance="?attr/textAppearanceListItem" />

                <TextView
                    android:id="@+id/content"
                    android:layout_gravity="center_horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/text_margin"
                    android:text="Shaaba"
                    android:textAppearance="?attr/textAppearanceListItem" />
            </LinearLayout>

        </LinearLayout>

    </LinearLayout>

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

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:srcCompat="@android:drawable/ic_menu_edit"
            android:id="@+id/imageButton4"
            android:layout_weight=".25" />

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:srcCompat="@android:drawable/ic_menu_delete"
            android:id="@+id/imageButton3"
            android:layout_weight=".25" />

        <Button
            android:text="@string/mark_as_default"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/button"
            android:layout_weight=".5" />
    </LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android android-layout android-recyclerview


    【解决方案1】:

    改变

      android:srcCompat="drawable/ic_menu_edit"
    

     app:srcCompat="drawable/ic_menu_edit"
    

    srcCompat 是 AppCompat 库的一个属性。

    【讨论】:

      【解决方案2】:

      您似乎正在尝试使用内置的 Android 资源。所以你需要在你的 ImageButton 图片来源:

      //改变这个

      <ImageButton android:src="@drawable/ic_menu_delete"/>
      

      //到这个

      <ImageButton android:src="@android:drawable/ic_menu_delete"/>
      

      确保在所有图像按钮的 srcCompat 属性中更改此语法,它应该可以工作。

      如果您的项目 drawables 文件夹中有这些图标的副本,那么 @drawable/ic_menu_delete 语法将起作用

      如果有任何帮助,请告诉我。

      【讨论】:

      • 使用 android:src 代替 android:srcCompat 再试一次。
      【解决方案3】:

      基本上原因是父 LinearLayout 中的第一个 LinearLayout 的 layout_height 为“match_parent”。因此,它抑制了包含三个 ImageButton 的 LinearLayout。将其更改为 wrap_content 使其工作正常。

      android:focusable="true"
          android:orientation="vertical">
      
          <LinearLayout
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多