2015-08-31 25 views
-1

我需要你的幫助。
我有一個圖像(例如http://www.gif.tv/tv.png),我想在它的中間放置我的相對佈局。帶背景圖像的定位元素Android

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:fbutton="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:background="@null"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="12dp" 
    android:paddingLeft="29dp" 
    android:paddingRight="35dp" 
    android:paddingTop="70dp" 
    android:paddingBottom="70dp" 
    android:background="@drawable/phone2"> 

問題是,當屏幕更改大小時,relativelayout太大/太小。
如何使relativelayout始終處於屏幕大小的任何位置?

+1

由於您提供給佈局的填充量會造成問題。 –

+1

使用android:src =「@ drawable/phone」的ImageView,而不是RelativeLayout。 – natario

+0

爲什麼你有兩個嵌套佈局? –

回答

0

使用

android:gravity="center" 

像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:fbutton="http://schemas.android.com/tools" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_marginLeft="5dp" 
android:layout_marginRight="5dp" 
android:gravity="center" 
android:background="@null"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="12dp" 
    android:paddingLeft="29dp" 
    android:paddingRight="35dp" 
    android:paddingTop="70dp" 
    android:paddingBottom="70dp" 
    android:background="@drawable/phone2"/> 
</LinearLayout> 
+0

謝謝,但它不起作用 – Skarwild

+0

使linearlayout作爲匹配的寬度和高度 – theLazyFinder

+0

檢查編輯答案 – theLazyFinder

0

這是你的扁平(沒有佈局嵌套!)佈局,中間一個TextView顯示出一些文字。
您可以使用具有一些複合繪圖的ImagView或TextView來混合文本和圖片。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:fbutton="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/tv" 
    > 
    <TextView 
     android:id="@+id/txtContents" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="16dp" 
     android:layout_centerInParent="true" 
     android:text="Some text contents here" 
    /> 
</RelativeLayout> 

android:layout_centerInParent="true"以水平和垂直中心爲中心。

您可能想要使用android:layout_centerVertical="true"android:layout_centerHorizontal="true"將居中限制爲只有其中一個大小。

+0

謝謝,但如何將元素不放置在中間? – Skarwild

+0

我以爲你問:'如何使relativelayout總是在任何屏幕大小?'無論如何...只要刪除'centerInParent'屬性,並使用填充和/或邊距和/或其他RelativeLayout對齊屬性(即:'' alignParentLeft') –