简体   繁体   English

(Android Studio)应用程序背景图像未填满屏幕

[英](Android Studio) App Background Image Doesn't Fill Screen

I'm new to app programming and require some assistance. 我是应用程序编程的新手,需要一些帮助。 I am building a simple app on Android Studio, but my app's background image (I named it "green_background") doesn't entirely fill the screen. 我正在Android Studio上构建一个简单的应用程序,但是我的应用程序的背景图像(我将其命名为“ green_background”)并未完全填满屏幕。 I can't provide a screenshot because I don't have 10 reputation on Stack Overflow. 我无法提供屏幕截图,因为我在Stack Overflow上没有10个声望。

Here is my app's activity coding. 这是我的应用程序的活动代码。 I have only added 1 piece of text, 2 buttons, 2 chronometers and 1 image view: 我只添加了1条文本,2个按钮,2个计时器和1个图像视图:

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/imageView"
    android:layout_alignRight="@+id/button2"
    android:layout_alignEnd="@+id/button2"
    android:contentDescription="@string/background_1"
    android:layout_alignParentEnd="@+id/button"
    android:layout_alignParentRight="@+id/button"
    android:background="@drawable/green_background"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />



<TextView
    android:text="@string/revision"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="60sp"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:layout_width="150dp"
    android:layout_height="80dp"
    android:text="@string/button_work"
    android:id="@+id/button"
    android:textSize="30sp"
    android:clickable="true"
    android:textColor="#ffe7e7e7"
    android:background="#d1131110"
    android:layout_alignTop="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<Button
    android:layout_width="150dp"
    android:layout_height="80dp"
    android:text="@string/button_break"
    android:id="@+id/button2"
    android:textSize="30sp"
    android:clickable="true"
    android:textColor="#ffe7e7e7"
    android:background="#d1131110"
    android:layout_below="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="59dp" />

<Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer"
    android:textSize="50sp"
    android:layout_below="@+id/button"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_marginTop="180dp" />

<Chronometer
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/chronometer2"
    android:textSize="50sp"
    android:layout_alignTop="@+id/chronometer"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

How do I make my background image (green_background) completely fill the phone screen? 如何使我的背景图像(green_background)完全填满手机屏幕?

I think It's happening because of the padding present in the Top level container..that is relative layout in your case If your Relative Layout looks like the below code 我认为这是由于顶层容器中存在填充而发生的..这就是您的情况下的相对布局,如果您的相对布局看起来像下面的代码

<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:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" >

or some thing very similar to that, remove these 4 lines of code 或类似的东西,删除这四行代码

android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"

Which applies some padding to your layout so the background is not completely visible. 这会将一些填充应用于您的布局,因此背景不能完全可见。

If the only use of that image View is to show the background..here is the simpler and preferred way of giving background to your layout 如果该图像的唯一用途是显示背景。这是为布局提供背景的更简单和首选的方法

<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/green_background"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="revision"
    android:textSize="60sp" />

<Button
    android:id="@+id/button"
    android:layout_width="150dp"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignTop="@+id/button2"
    android:background="#d1131110"
    android:clickable="true"
    android:text="button_work"
    android:textColor="#ffe7e7e7"
    android:textSize="30sp" />

<Button
    android:id="@+id/button2"
    android:layout_width="150dp"
    android:layout_height="80dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView"
    android:layout_marginTop="59dp"
    android:background="#d1131110"
    android:clickable="true"
    android:text="button_break"
    android:textColor="#ffe7e7e7"
    android:textSize="30sp" />

<Chronometer
    android:id="@+id/chronometer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button"
    android:layout_alignStart="@+id/button"
    android:layout_below="@+id/button"
    android:layout_marginTop="180dp"
    android:textSize="50sp" />

<Chronometer
    android:id="@+id/chronometer2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/chronometer"
    android:textSize="50sp" />

Note that the image view is removed and background for the entire layout is applied by the single line of code android:background="@drawable/green_background" in the relative layout 请注意,图像视图已删除,整个布局的android:background="@drawable/green_background"由相对布局中的单行代码android:background="@drawable/green_background"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM