我的 Activity 和其中的 ImageButton 有问题。看起来像是在剪裁:
这是相应 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 中的布局设计器会正确显示布局:
这里的问题在哪里?我已经尝试更改边距或填充值,但按钮仍在我的 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/