2016-08-22 26 views
0

我試圖設計佈局(基本上是一個閃屏),顯示在屏幕的幾乎中間和標識下的公司標語在App標誌設置應用程序的開始。在Android Studio中工作時,佈局設計看起來像這樣。我從頂部向下定位了200dp。輸出設計並不確切是什麼,同時發展佈局

正如您所看到的,標誌正好位於屏幕中間。 但是,當我在我的模擬器上運行應用程序時,屏幕顯示如下。標誌不在我想要的確切位置。

我想我在相同的相對位置的標誌,無論屏幕尺寸多大。

假設,如果標誌位於200dp在1000dp長的屏幕。我希望它在2000dp屏幕上的400dp位置。

模擬器:5.0.0 API-21 768×1280

什麼是可能的解決方案?

以下圖像包含兩個輸出。

Follow this Link for the image

下面是XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#f84343" 
android:weightSum="1"> 

<ImageView 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:id="@+id/AppIcon" 
    android:src="@drawable/unnamed" 
    android:layout_marginTop="200dp" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/simpler_better_faster" 
    android:id="@+id/textView" 
    android:layout_gravity="center_horizontal" 
    android:layout_below="@+id/AppIcon" 
    android:layout_centerInParent="true" 
    android:layout_marginTop="50dp" 
    android:textColor="#ffffff" 
    android:textSize="20dp" /> 
</RelativeLayout> 
+0

添加相關的XML代碼。 –

回答

0

你並不需要使用<RelativeLayout>來實現這一目標。以下是使用<LinearLayout>的代碼。這裏的關鍵是在<LinearLayout>元素android:gravity="center"

<?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="#f84343" 
       android:orientation="vertical" 
       android:gravity="center"> 

    <ImageView 
     android:id="@+id/AppIcon" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:src="@drawable/unnamed"/> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:text="@string/simpler_better_faster" 
     android:textColor="#ffffff" 
     android:textSize="20dp"/> 

</LinearLayout> 

[編輯]

OP - 但是,如果我使用更多的widget屏幕去我可能需要 位置它們相對於彼此。如何克服與 相對佈局

那麼,在技術上仍然可以在他們需要的順序添加更多的widget。但是,如果你真的想用一個<RelativeLayout>,您可以使用類似:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#f84343" 
       android:gravity="center_vertical"> 

    <ImageView 
     android:id="@+id/AppIcon" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:layout_centerHorizontal="true" 
     android:src="@drawable/unnamed"/> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="20dp" 
     android:layout_below="@id/AppIcon" 
     android:text="@string/simpler_better_faster" 
     android:textColor="#ffffff" 
     android:layout_centerHorizontal="true" 
     android:textSize="20dp"/> 

</RelativeLayout> 
+0

工作。但是如果我繼續使用更多的小部件到屏幕上,我可能需要將它們相對放置。如何用相對佈局來克服? –

+0

@AyanBansal查看我的更新 – Shaishav

+0

我更新了問題。請看看 –

0

它看起來像你混淆了一些RelativeLayout/LinearLayout屬性。

如果你想使用的RelativeLayout刪除和修改一些屬性。

像這樣的事情,而不是

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f84343"> 

    <ImageView 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:id="@+id/AppIcon" 
     android:src="@drawable/unnamed" 
     android:layout_centerInParent="true"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/simpler_better_faster" 
     android:layout_below="@id/AppIcon" 
     android:id="@+id/textView" 
     android:layout_marginTop="50dp" 
     android:textColor="#ffffff" 
     android:textSize="20dp" /> 
</RelativeLayout> 
+0

我更新了問題。請考慮一下。 –

0

簡化了這一切變成一個TextView如果你問我

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#f84343" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:text="@string/simpler_better_faster" 
     android:drawableTop="@drawable/arrow_right_fill_dark" /> 

</RelativeLayout> 

這擺脫了ImageView的所有一起,改變作爲drawableTop屬性提供一個可繪製的,集中在文本上方。