【问题标题】:Android Imagebutton is clippedAndroid Imagebutton 被剪裁
【发布时间】:2018-09-25 20:08:05
【问题描述】:

我的 Activity 和其中的 ImageButton 出现问题。它看起来像是在剪裁:

这是对应活动的XML:

<?xml version="1.0" encoding="utf-8"?>
<!-- A RecyclerView with some commonly used attributes -->
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="@+id/todo_linear_layout"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="horizontal">

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        app:srcCompat="@android:drawable/ic_input_add" />

    <EditText
        android:id="@+id/edittodo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20px"
        android:layout_weight="5"
        android:textColor="@android:color/black" />
</LinearLayout>

<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/todo_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >

</android.support.v7.widget.RecyclerView>
</LinearLayout>

此外,Android Studio 中的布局设计器正确显示布局:

问题出在哪里?我已经尝试更改边距或填充值,但 Button 仍在我的 Android 设备上正在运行的应用程序中剪辑。

【问题讨论】:

  • 您在 ImageButton 中使用矢量右图像?
  • 当我尝试它时它工作正常。您是否尝试将 srcCompat 设置为 ic_input_add 以外的其他值?真的很奇怪,因为这个图标从最早的安卓版本就出现了
  • @nupadhyaya 是的,我做到了。例如,我尝试使用另一个加号(我不知道确切的名称 atm)
  • @NKnuelle 尝试为线性布局提供高度或使用 android:src 而不是 app:srcCompat

标签: android android-layout android-linearlayout android-imagebutton


【解决方案1】:

我相信正在发生的事情是您运行应用程序的设备没有@android:drawable/ic_input_add drawable。

我尝试运行您发布的代码,一切正常。但是,如果我从 &lt;ImageButton&gt; 标记中删除 app:srcCompat 属性,我会得到与您在第一个屏幕截图中发布的相同的行为。

通常,您不能 100% 依赖具有 @android: 资源的设备。一些制造商删除了资源,而另一些制造商则用废话替换了这些值(例如,我看到 @android:color/white 显示为灰色)。

我建议您创建自己的可绘制对象(甚至可以手动从 Android 复制一个并将其添加到您的项目中),然后引用它。

app:srcCompat="@drawable/your_own_add"

【讨论】:

  • 这似乎是问题所在。我明天试试。我从没想过,Android 资源不能出现在 Android 设备上。实际上听起来很奇怪?
【解决方案2】:

将 app:srcCompat 更改为: android:src="@android:drawable/ic_input_add" 做到了!所以问题是,设备没有找到那个图标,只显示灰色的东西。

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2018-10-22
    • 2021-07-15
    • 1970-01-01
    • 2021-05-05
    • 1970-01-01
    相关资源
    最近更新 更多