如何使按钮的前台属性在API 23下工作?

Kum*_*mar 5 java xml android foreground android-layout

我有两个Buttons嵌套在一个LinearLayout。在这Buttons两者之间TextViews。在中Xml,我为每一个图像都设置了前景Buttons

在我的设备上运行正常Api 23。但是在下面的其他设备上Api 23,前景图像不显示,而是导致默认的白色纯色。有什么办法可以使用下面的前景显示这些图像Api 23

我们已经尝试过了,FrameLayout但是它没有做我们想要做的事情。会ImageButtons是一个更好的办法来解决这个问题?

我们应用程序的核心功能之一是,每次用户点击a时Button,大小都会增加,并且图像会相应拉伸。这是在代码中动态完成的。如果要使用ImageButtons,则每次都需要设置高度和宽度的布局参数,而不是一行代码来设置高度。

任何提示将不胜感激!

编辑:我正在使用的代码-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="11"
    android:background="@android:color/black">

    <Button
        android:layout_weight="5"
        android:id="@+id/firstP"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:foreground="@drawable/icebutton"
        android:scaleX="1"
        android:scaleY="1"/>

    <TextView
        android:layout_weight="0.5"
        android:id="@+id/firstPlayer"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:rotation="180"
        android:textColor="@android:color/white"
        android:background="@android:color/transparent"/>

    <TextView
        android:layout_weight="0.5"
        android:id="@+id/secondPlayer"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:background="@android:color/transparent"/>

    <Button
        android:layout_weight="5"
        android:id="@+id/secondP"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:foreground="@drawable/firebutton"
        android:scaleX="1"
        android:scaleY="1"/>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Kum*_*mar 4

我们发现有两个问题导致图像无法显示。
1. 图像文件太大,导致错误outOfMemory,导致按钮不显示图像。
2.前台属性不适用于API 22及以下版本。

解决这些问题的步骤:
1. 我们减小了图像文件的大小。
2.我们替换ButtonImageButton
3。在XML文件中我们删除了前景属性,添加了黑色背景,并通过属性添加了图像src。以下是一个片段。

<ImageButton
    android:layout_weight="5"
    android:id="@+id/firstP"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:src="@drawable/icebutton"
    android:scaleType="fitXY"
    android:background="@android:color/black"/>
Run Code Online (Sandbox Code Playgroud)
  1. 然后,我们必须更改代码以动态调整按钮的高度,以在此链接的帮助下通过设置来匹配新的图像按钮LayoutParams
    How to Change Size of Button Dynamic in Android

现在一切都很完美!