如何在 Android UI 中设置布局背景

新手上路,请多包涵

我是 Android 编程的新手。我有一个带有一些 TextViewButton 控件的 UI。如何在这些组件后面设置背景?让我们称之为 background.png

原文由 farissyariati 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 293
2 个回答

在您的父布局元素中,例如 linearlayout 或其他元素,只需添加 android:background="@drawable/background" 这将设置布局的背景,假设您的图像位于 /drawable 文件夹中。

原文由 LuxuryMode 发布,翻译遵循 CC BY-SA 3.0 许可协议

First you have to place your background.png image in your res/drawable/ folder.Later you have to set a parent layout for your TextView and Button widgets.I 将考虑 LinearLayout 作为您的父布局,代码如下所示:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:background="@drawable/background.png"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent">
 <TextView    android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/hello" />
 <Button      android:text="Button"
              android:id="@+id/button1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"></Button>
</LinearLayout>

原文由 Vivek Kalkur 发布,翻译遵循 CC BY-SA 3.0 许可协议

推荐问题