gpt4 book ai didi

Android:将图像添加到具有形状背景的按钮

转载 作者:行者123 更新时间:2023-11-30 03:04:33 25 4
gpt4 key购买 nike

我是 android 的新手,目前正在编写我的第一个应用程序,但我已经为一件事苦苦挣扎了两天:我有一个按钮,其形状来自 xml 文件作为背景。除一个按钮外,每个按钮上都会有文字。有图像的那个让我头疼——我使用了形状背景并将图像放在 TextView: drawableTop 中,但它的大小与其他所有按钮都不同,我需要它们的大小完全相同。这是代码:

<LinearLayout

<Button
android:id="@+id/ButtonCoffee"
style="@style/buttonText"
android:layout_height="wrap_content"
android:drawableTop="@drawable/coffeepng"
android:focusable="false"
android:onClick="loadBreak"
android:textSize="@dimen/activity_vertical_margin" />

我为所有按钮使用的样式:

<style name="buttonText" parent="@android:style/TextAppearance">
<item name="android:textColor">#FFFFFF</item>
<item name="android:textSize">35sp</item>
<item name="android:typeface">sans</item>
<item name="android:textStyle">bold</item>
<item name="android:background">@drawable/btn_background_green</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_marginTop"> 5dp </item>
<item name="android:layout_marginBottom">5dp </item>
</style>

同时我想再问几个我在开发这个应用程序时遇到的问题:

  1. 使用形状作为按钮背景效率更高还是使用 png 图像更好?在不同设备上大小相同是否重要?

  2. 是否可以进行多个 Activity ,或者我应该尝试在单个 Activity 上绘制不同的按钮集(取决于用户的样式选择)?

  3. 如何将图像从一个 Activity 传递到另一个 Activity ?

最佳答案

drawable 文件夹中创建一个名为 button_shape.xml 的形状,如下所示...

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<solid android:color="#ef4444" />

<stroke
android:width="1dp"
android:color="#992f2f" />

<corners android:radius="16dp" />

</shape>

然后使用这个形状设置为按钮的背景如下...

<Button
android:id="@+id/Button1_2"
style="@style/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:onClick="load1_2"
android:text="@string/btn1_2"
android:drawableTop="@drawable/coffeepng"
android:background="@drawable/button_shape" />

关于Android:将图像添加到具有形状背景的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22014156/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com