2017-04-03 81 views
1

我在启动屏幕活动中使用了一个图像作为LinearLayout的背景,但它显示不适合屏幕。我不知道如何使用属性来扩展它。LinearLayout中的初始屏幕图像背景不适合

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_splash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.myproject.SplashActivity" 
    android:orientation="horizontal" 
    android:background="@drawable/my_image_background"> 

</LinearLayout> 

回答

1

正确的方法是这样的所以用这个来代替,在styles.xml补充一点:

<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar"> 
    <item name="android:windowBackground">@drawable/splash</item> 
</style> 

而在你的清单文件添加此

<activity 
     android:name=".SplashActivity" 
     android:theme="@style/SplashTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

和@绘制/飞溅可能像

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:opacity="opaque"> 
    <!-- The background color, preferably the same as your normal theme --> 
    <item android:drawable="@drawable/splashscreen"/> 
    <!-- Your product logo - 144dp color version of your app icon --> 
    <item> 
     <bitmap 
      android:src="@drawable/splash_logo" 
      android:gravity="center"/> 
    </item> 
</layer-list> 
1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/dighsplash"> 
</LinearLayout> 

试试这种方式。

1
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_splash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.myproject.SplashActivity" 
    android:orientation="horizontal" 
    android:background="@drawable/my_image_background"> 
    <ImageView 
     android:scaleType="fitXY" 
     android:src="@drawable/my_image_background" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</LinearLayout> 

使用ImageView并使用属性scaleType。这将自动适合任何屏幕分辨率。

-----更新-------

至于建议的Ahamed除此之外,如果你想要更多的意见添加到布局,它更好地使用RelativeLayout(自它会叠加你的视图)。考虑下面的RelativeLayout的实现。

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_splash" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:scaleType="fitXY" 
      android:src="@drawable/my_image_background" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
      <!-- Add other views depending on your screen specs --> 
    </RelativeLayout> 
+0

他需要背景图片。如果他在Linearlayout中设置了imageview,他如何放置其余的布局?你能解释一下吗? – Noorul

+0

@Ahamed我刚刚给出了一个启动画面的通用示例。可以使用RelativeLayout而不是LinearLayout来解决您所陈述的问题。 –

+1

是的。你做到了。我很感激。但是,一些新的基本程序员不知道下一步该怎么做。所以,给你一些关于你的答案的更多信息,那么你将获得更好的声誉。 – Noorul

1

最好使用ImageView的图像,所以我已经改变了你的布局如下&您预期的那样工作,建议立即进行删除。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_splash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    tools:context="com.myproject.SplashActivity"> 

    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scaleType="fitXY" 
     android:src="@drawable/my_image_background" /> 

</LinearLayout> 
0

如果scaleType =“fitxy”不适用于imageview,请尝试以下操作。

机器人:fitSystemWindows = “真”

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_splash" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.myproject.SplashActivity" 
android:orientation="horizontal" 
android:background="@drawable/my_image_background" 
android:fitSystemWindows="true"> 
+2

这里有什么需要fillviewport = true。我认为这是scrollview属性。编辑你的答案。 – Noorul

+0

是的,你是对的。谢谢 – Pehlaj

1

使用此:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/activity_splash" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.myproject.SplashActivity" 
android:fitSystemWindows="true" 
android:orientation="horizontal" 
android:scaleType="fitXY" 
android:background="@drawable/my_image_background"> 

此代码绝对适合您的屏幕。

1

首先检查图像my_image_background对双方任何空格。如果没有,你的代码工作...否则ü可以试试这个代码太

<?xml version="1.0" encoding="utf-8"?> 
     <RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:id="@+id/activity_splash" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      tools:context="com.myproject.SplashActivity" 
      android:orientation="vertical" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentBottom="true" 
      android:background="@drawable/my_image_background"> 

     </RelativeLayout> 

退房这也::: Showing Splash screen in android apps