2017-09-17 22 views
0

我已經使用android developer studio創建了以下初始屏幕布局。我想要將由兩個文本視圖和一個圖像視圖組成的內容居中。以下是XML。以android線性佈局居中內容的最佳方式

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/splash_gradient"> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <ImageView id="@+id/splashscreen" 
     android:layout_width="100dp" 
     android:layout_height="250dp" 
     android:paddingBottom="100dp" 
     android:layout_centerInParent="true" 
     android:gravity="center_vertical|center_horizontal" 
     android:src="@drawable/splash_logo" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Sample" 
     android:layout_centerInParent="true" 
     android:fontFamily="@string/font_family_thin" 
     android:paddingTop="90dp" 
     android:paddingRight="90dp" 
     android:textSize="35dp" 
     android:textColor="#fff" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="App" 
     android:layout_centerInParent="true" 
     android:fontFamily="@string/font_family_regular" 
     android:textSize="35dp" 
     android:paddingTop="90dp" 
     android:paddingLeft="100dp" 
     android:textColor="#fff" 
     /> 

</RelativeLayout> 

這看起來像下面的圖片。

enter image description here

這是正確的方式居中我的內容或是否有更好的方式來實現我在尋找什麼呢?請指導我。提前致謝。

回答

1

如果您使用填充將元素居中,則這是錯誤的方式,因爲您將在不同的屏幕上獲得不同的結果。

看到這個選擇,它不是唯一的一個。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/splash_gradient" 
    android:gravity="center"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center"> 

     <ImageView 
      android:id="@+id/splashscreen" 
      android:layout_width="100dp" 
      android:layout_height="250dp" 
      android:paddingBottom="100dp" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:src="@drawable/splash_logo" /> 

     <LinearLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/splashscreen" 
      android:layout_centerHorizontal="true" 
      android:orientation="horizontal" 
      android:layout_marginTop="10dp"> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sample" 
       android:textSize="35dp" 
       android:textColor="#fff" 
       android:layout_marginEnd="15dp" 
       android:layout_marginRight="10dp" 
       android:fontFamily="@string/font_family_thin" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="App" 
       android:layout_centerInParent="true" 
       android:textSize="35dp" 
       android:textColor="#fff" 
       android:fontFamily="@string/font_family_regular" 
       /> 
     </LinearLayout> 
    </RelativeLayout> 
</LinearLayout> 
+0

我改了一下你的答案讓我的精確設計和它看起來完美。你能指示我有關如何使用此android佈局獲得令人驚歎的UI的任何文檔!我搜索了一下,發現只有基本的教程。 –

+1

我不記得任何教程特別。但是搜索特定於每個佈局的Tutorias:FrameLayout,LinearLayout,RelativeLayout和ConstraintLayout。最後一個是擁有更多選項但也是最複雜的那一個,所以我會先消化熟悉之前的那些。 – Juan

+0

噢,好的。非常感謝你! :) –

0

先不使用填充或保證金對齊相對於兄弟姐妹的意見或佈局圖,它將在1臺設備看起來很棒,但不會在不同屏幕尺寸的其他設備。使用layout_align屬性。瞭解線性佈局屬性here。然後,你將能夠通過你自己發現最好的方式。

我的解決方案

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="100dp" 
      android:layout_height="250dp" 
      android:layout_centerInParent="true" 
      /> 
     <LinearLayout 
      android:layout_centerInParent="true" 
      android:layout_below="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/sample" 
       android:layout_alignBottom="@+id/image" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sample" 
       android:layout_centerInParent="true" 
       android:textSize="35dp" 
       android:textColor="#000" 
       /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text=" App" 
       android:textSize="35dp" 
       android:textColor="#000" 
       /> 

     </LinearLayout> 




</RelativeLayout>