gpt4 book ai didi

Android:使用背景png图像

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:11 26 4
gpt4 key购买 nike

我正在寻找使用 .png 图像作为应用程序主 Activity 屏幕背景的最佳方式。我希望能够在尽可能多的设备上正确安装。

我所做的工作,我想知道的是可移植性的最佳实践。

这是我在 XML 中使用的代码:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_background"
android:padding="25dp"
>

...other stuff like a few buttons that go on top of background

当前文件“main_background.png”是一张 800 像素 x 600 像素的图像。我注意到在我的手机上,至少水平尺寸向内缩小,从而扭曲了图像。是否有理想的 Width::Height 比例可以制作我的背景图片?

谢谢。

最佳答案

您要做的是像这样设置您的 xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@drawable/main_background"
android:padding="25dp"
>

注意缺少 layout_height...

在您的主 Activity 中,您会找到该 View ,并执行以下行:

myLinLayout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, myLinLayout.getWidth()));

其中myLinLayout是线性布局,你按照LayoutParams方法:

setLayoutParams(int width, int height);

关于Android:使用背景png图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14114556/

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