2017-09-17 9 views
0

アンドロイドデベロッパースタジオを使用して、次のスプラッシュ画面レイアウトを作成しました。私は、2つのテキストビューとイメージビューからなるコンテンツを中心にしたいと思っていました。以下はXMLです。アンドロイドのリニアレイアウトにコンテンツを配置する最も良い方法

<?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> 
関連する問題