2017-10-08 86 views
0

我的確切目的閃屏的中心規模大小,地點圖像是減少圖像的大小(高度和寬度)略(如WRAP_CONTENT尺寸過大),並把它放在中心初始屏幕。但是不管我做什麼,它只是在我使用wrap_content或match_parent之前不會居中。這是我使用的代碼。如何在Android應用程序

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.yoalfaaz.yoalfaaz.SplashScreen"> 

    <ImageView 
     android:layout_width="300dp" 
     android:layout_height="300dp" 
     tools:layout_editor_absoluteY="25dp" 
     tools:layout_editor_absoluteX="25dp" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:src="@drawable/ss" /> 
</android.support.constraint.ConstraintLayout> 

什麼是最好的方式去做它,因爲設置一個前綴大小,因爲我不會讓它適應更大的屏幕(主要是平板電腦)。

+0

什麼是父面板?線性?相對? –

+0

@ d.moncada你能告訴我該如何檢查。我仍然是Android的初學者,所以我很困惑。雖然我編輯了這個問題以提供有關activity_splash_screen.xml文件的所有內容,但也許可以提供幫助。 –

+0

您的編輯說明我需要什麼,感謝 –

回答

1

既然你正在使用你的父母面板constraintlayout,請嘗試以下操作:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.yoalfaaz.yoalfaaz.SplashScreen"> 
    <ImageView 
     android:layout_width="300dp" 
     android:layout_height="300dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintBottom_toBottomOf="parent" 
     android:src="@drawable/ss" /> 
</android.support.constraint.ConstraintLayout> 
+0

你的答案釘它完美。你能告訴我你到底做了什麼,這樣我可以更好地理解它。 :) –

+1

由於您使用的是ConstraingLayout,因此我在圖像的所有面(toLeftOf,RightOf,toTopOf和toBottomOf)中添加了約束條件。這將迫使它在中心。 –

+0

我明白了,非常感謝您的回答。 –

0

嘗試加入您的ImageView內的LinearLayout或相對佈局的父母。

然後與Android添加ImageView的:scaleType =「中心」,也不要忘了,如果使用相對或Android添加centerInParent =真:比重=「中心」的LinearLayout。

1

如果你正在使用的LinearLayout作爲父母,那麼你需要的重力爲中心設置爲你的父母的LinearLayout設置兒童如下所示

 <LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:gravity="center" 
        android:orientation="vertical"> 
        <ImageView 
         android:layout_width="300dp" 
         android:layout_height="300dp" 
         android:layout_gravity="center" 
         tools:layout_editor_absoluteY="25dp" 
         tools:layout_editor_absoluteX="25dp" 
         android:src="@drawable/ss" 
         /> 
       </LinearLayout> 
// if you are using RelativeLayout then : 
     <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:layout_width="300dp" 
      android:layout_height="300dp" 
      android:layout_gravity="center" 
      tools:layout_editor_absoluteY="25dp" 
      tools:layout_editor_absoluteX="25dp" 
      android:src="@drawable/ss" 
      android:layout_centerInParent="true" 
      /> 
    </RelativeLayout> 
+0

你的回答也是完美的,我只需要從約束佈局更改爲線性或相對的。 –

相關問題