如何使按钮的前景属性在 API 23 以下工作?
How do I make the foreground attribute for a button work below API 23?
我有两个 Buttons
嵌套在一个 LinearLayout
中。在这些 Buttons
之间有两个 TextViews
。在 Xml
中,我已将前景设置为每个 Buttons
的图像。
它在我的设备上运行良好 Api 23
。但在 Api 23
以下的其他设备上,前景图像不会显示,而是会显示默认的白色纯色。有什么方法可以让这些图像在 Api 23
下面使用前景显示吗?
我们已经尝试 FrameLayout
但它没有按照我们的要求进行。 ImageButtons
是解决此问题的更好方法吗?
我们应用程序的核心功能之一是,每次用户点击 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>
我们发现有两个问题导致图片无法显示。
1. 图像文件太大,造成outOfMemory
错误,进而导致按钮不显示图像。
2. foreground属性对API22及以下无效
解决这些问题的步骤:
1. 我们减小了图像文件的大小。
2.我们将Button
替换为ImageButton
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"/>
- 然后我们不得不更改我们的代码以动态调整按钮的高度以在这个 link 的帮助下通过设置
LayoutParams
来匹配新的图像按钮:
how to change size of button dynamic in android
现在一切正常!
我有两个 Buttons
嵌套在一个 LinearLayout
中。在这些 Buttons
之间有两个 TextViews
。在 Xml
中,我已将前景设置为每个 Buttons
的图像。
它在我的设备上运行良好 Api 23
。但在 Api 23
以下的其他设备上,前景图像不会显示,而是会显示默认的白色纯色。有什么方法可以让这些图像在 Api 23
下面使用前景显示吗?
我们已经尝试 FrameLayout
但它没有按照我们的要求进行。 ImageButtons
是解决此问题的更好方法吗?
我们应用程序的核心功能之一是,每次用户点击 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>
我们发现有两个问题导致图片无法显示。
1. 图像文件太大,造成outOfMemory
错误,进而导致按钮不显示图像。
2. foreground属性对API22及以下无效
解决这些问题的步骤:
1. 我们减小了图像文件的大小。
2.我们将Button
替换为ImageButton
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"/>
- 然后我们不得不更改我们的代码以动态调整按钮的高度以在这个 link 的帮助下通过设置
LayoutParams
来匹配新的图像按钮:
how to change size of button dynamic in android
现在一切正常!