【问题标题】:Layout: background picture stretched布局:背景图片拉伸
【发布时间】:2023-12-03 05:53:01
【问题描述】:

我的布局有问题。

作为我的第一个开发项目,我正在尝试制作一个简单的计时器。 我想在背景中有一张计时器的照片以及开始和停止按钮 在应用程序的底部,并排并填充屏幕的宽度。

这是我最好的镜头,但我并不满意。

所有小部件都已正确放置,但... 我的背景被拉伸了,我的按钮看起来被垂直压缩了,甚至底部的文字也被剪掉了一点。

我发现如果我改变这些行

android:layout_width="fill_parent"
android:layout_height="fill_parent"

通过

android:layout_width="wrap_content"
android:layout_height="wrap_content"

背景还可以,按钮也可以..但是所有的小部件都放在了Activity的中心。

我该如何解决这个问题?

顺便说一句,如果我想让我的按钮并排,我是否选择了更好的解决方案?

感谢您的帮助! 查尔斯。

...现在是我的xml ....

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@drawable/background"
tools:context=".Chronosv1" >

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:layout_weight="2" >

<Chronometer
    android:id="@+id/chronometer1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" />

</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_weight="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="START" />

     <Button
        android:id="@+id/button2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="STOP" />
</LinearLayout>

我尝试了相对布局。 我不知道如何在不使用填充的情况下拥有两个大小相同的按钮,如果您希望应用在不同的屏幕上运行,我认为这不是一个好主意。

无论如何我想出了这个,但我仍然有我的拉伸图像,我的按钮没有相同的大小。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
tools:context=".Chronosv1" >

<Chronometer
    android:id="@+id/chronometer1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:text="Start" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/button1"
    android:text="Stop" />

【问题讨论】:

  • 1) 很可能,您需要比LinearLayout 更高级的东西; (2) 你的Chronometer 组件是什么?
  • Chronmeter 是一个 android 小部件 android.widget.Chronometer

标签: android android-layout android-widget


【解决方案1】:

试试RelativeLayout ;) 我认为这比LinearLayout 更容易处理。也许你可以上传一张图片它的样子,我会给你一个相对布局的解决方案;)

【讨论】:

  • 我对这一层有一些困难,我发现了如何将按钮并排放置,但不知道如何填充屏幕的所有宽度。我附上了我的屏幕快照。
【解决方案2】:

你可以试试这个吗?应该工作海事组织。 这是获取布局的更好/更简单的方法。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".Chronosv1" >


<LinearLayout
android:id="@+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_alignParentBottom="true">
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:text="Start" />

<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
android:text="Stop" />
</LinearLayout>
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout1" />
</RelativeLayout> 

【讨论】:

  • 按钮没问题,但背景还是steched。
  • 我注意到顶部标题栏(带有应用程序名称的标题栏)对背景图片有影响......我不明白为什么但我的 320x240 图片无法正确显示,因为其中。我试图将我的图片转换为 9 补丁图像。现在在 Eclipse/Graphical Layout 中一切正常,但在我的 AVD 和手机上,其他组件不再显示...... grrrrrr。
【解决方案3】:

我终于找到了解决办法。

第一个问题:我的 9 补丁图像隐藏了我的其他组件。

解决方案:在您的线性布局中添加 android:padding="0dip" ...这里是给我答案的主题 (Android layout broken with 9-patch background)

第二个问题:为什么我的图片被拉伸了,而我却按照屏幕的确切尺寸设计了它。这是一个愚蠢的问题......这是因为上面的操作栏和另一个栏(电池电量和其他东西)。

要摆脱这些栏(包含在主题中),请在清单中使用不同版本的主题。

我选择:Theme.Light.NoTitleBar.Fullscreen。

问题解决了。

【讨论】: