2012-02-15 110 views
1

作爲一名android開發人員,我非常新,我目前瞭解RelativeLayout,但面臨的問題是Textview overLapping在圖像上。在這裏,我向你提供圖像,我想要什麼,我得到什麼,還有我已經做了很多的代碼。所以請幫助我。相對佈局覆蓋在RelativeLayout組件中聲明的組件

<RelativeLayout 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/firstImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/splash1" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/firstImage" 
     android:layout_alignBaseline="@id/firstImage" 
     android:text="@string/menu" /> 

    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/splash1" /> 
</RelativeLayout> 

這是我想要的佈局圖像。

enter image description here

請幫助我。

在此先感謝..

+0

我會使用LinearLayout。 http://developer.android.com/resources/tutorials/views/hello-linearlayout.html – Sparky 2012-02-15 09:26:56

回答

3

使用這些屬性

android:layout_centerHorizontal="true"  
android:layout_centerVertical="true" 

文本視圖..

檢查這個代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

<ImageView 
    android:id="@+id/firstImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/logo2"/> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/logo2" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"  
    android:layout_centerHorizontal="true"  
    android:layout_centerVertical="true"  
    android:text="menu" /> 

+0

謝謝Raja很好的答案 – 2012-02-15 09:44:46

1

再添加兩個TextView的android:layout_centerHorizontal="true"android:gravity="center"爆破性質,所以文本將YOUT TextView的內部居中。

如果文字太大(它會重疊圖像),可能會出現問題。爲了避免你應該這樣定義你的佈局:

<RelativeLayout 
android:id="@+id/relativeLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
    <ImageView 
    android:id="@+id/firstImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/splash1" 
    /> 
    <ImageView 
    android:id="@+id/secondImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentTop="true" 
    android:src="@drawable/splash1" 
    /> 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_alignTop="@id/firstImage" 
    android:layout_alignBottom="@id/firstImage" 
    android:layout_toRightOf="@id/firstImage" 
    android:layout_toLeftOf="@id/secondImage" 
    android:gravity="center" 
    android:text="@string/menu" 
    /> 
</RelativeLayout> 

請注意,圖像視圖如何在TextView之前定義。這是因爲這些圖像視圖將成爲您的錨點視圖,這意味着所有其他視圖都將根據它們對齊。你的TextView現在在2個ImageViews之間,所以不管它有什麼文字,它都不會重疊任何東西。

1

替換此XML文件的代碼...

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/firstImage" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/splash1" /> 

    <ImageView 
     android:id="@+id/firstImage2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/splash1" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_toLeftOf="@+id/firstImage2" 
     android:layout_toRightOf="@+id/firstImage" 
     android:text="@string/menu" /> 

</RelativeLayout> 
1

您可以切換到圖形模式並修復上述問題。並不是你必須直接用XML定義所有的東西,但你可以切換到圖形模式並修復它。