Android Imagebutton被剪裁

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

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

enter image description here

这是相应 Activity 的 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 中的布局设计器会正确显示布局:

enter image description here

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

最佳答案

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

我尝试运行您发布的代码,一切正常。但是,如果我删除 app:srcCompat来自 <ImageButton> 的属性标记,然后我得到与您在第一个屏幕截图中发布的相同的行为。

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

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

app:srcCompat="@drawable/your_own_add"

关于Android Imagebutton被剪裁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52505950/

相关文章:

android - 将布局参数设置为 ImageView 时出错

android - Google Maps Android API v2 应用程序崩溃

没有第一个字符的Android linkify

java - 生成的 .apk 包含未使用的资源和未使用的类

android - 图片预览太长

android - 带工具的原因是什么 :context in parent layout?

java - Android View getWidth方法总是返回0

android - 如何使用 Authenticator 和挂起 api 功能?改造

android - 从右到左 Android 应用中的选项卡滑动方向

android - 让 slider /线性布局的未显示部分不调整大小