2014-09-29 44 views
0

我要實現這一點: Android:如何製作兩個視圖align-center?

我應該怎麼做才能讓藍色的「+」對準中心與橙色區域的底線?非常感謝。

+0

你可以張貼一些代碼?你試過什麼了? – 2014-09-29 03:36:23

+0

發佈您的代碼。 – CENT1PEDE 2014-09-29 03:37:57

+0

我只在它下面有一個橙色的Linearlayout和一個TextView。我不知道如何處理藍色加。 @Amulya Khare – hanswim 2014-09-29 03:41:12

回答

2

一個解決方案是使用代碼。設置「+」視圖的頂部填充,它應該是:

topPadding = orangeHeight - plusViewHeight/2

當然,你應該做的,經過外觀的看法,因此可以返回你非零高度值。

0

你可以設置你想要的高度。請確保您設置的ImageView

屬性:

android:layout_centerVertical="true" 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" > 

     <TextView 
      android:id="@+id/textView2" 
      android:layout_width="wrap_content" 
      android:layout_height="100dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" 
      android:gravity="right" 
      android:background="#d34520" 
      android:textColor="#fffff" 
      android:text="HTML 5 everywher....." /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="100dp" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:gravity="right" 
      android:text="The web platform....." /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="22dp" 
      android:src="@drawable/plus" /> 

    </RelativeLayout> 
+0

固定佈局的高度在所有設備上永遠不會好。更好地爲不同的屏幕尺寸切片圖像使用'wrap_content'或'match_parent'來同時顯示不同的設備。 – XtreemDeveloper 2014-09-29 05:27:22

+0

是的,我同意你的固定高度不好。然而,對於各個尺寸的佈局文件可能是一個不錯的選擇。添加圖像可能會增加應用程序的大小。 – Dhina 2014-09-29 05:31:24

+0

兩個TextView的文本不是永久的,它可能非常長或很短。所以我認爲這種方式不是很好。 – hanswim 2014-09-29 06:24:35

0

這是所有關於你的設計技巧,如果你想實現你的目標。按照下面的步驟:

步驟1:)

設計命名爲1的四個圖像btn_back.png 2)lay1.png 3)lay2.png和4)btn_plus.png它將看起來像這樣:

1) btn_back

enter image description here

2)lay1.png

enter image description here

3)lay2.png

enter image description here

4)btn_plus.png

enter image description here

第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:orientation="vertical" > 

    <LinearLayout 
     android:id="@+id/lay1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/lay1" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="30dp" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Type your text here what ever you want..." /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/btn_back" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/btn_back" 
     android:gravity="left" 
     android:orientation="vertical" > 

     <Button 
      android:id="@+id/btnPlus" 
      android:layout_width="50dp" 
      android:layout_height="45dp" 
      android:layout_margin="5dp" 
      android:background="@drawable/btn_plus" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/lay2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/lay2" 
     android:gravity="center" 
     android:orientation="vertical" 
     android:padding="30dp" > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="Type your text here what ever you want..." /> 
    </LinearLayout> 

</LinearLayout> 

和你做,你會得到期望的結果:)

+0

這種方式的作品,但它不是很好。因爲加號的底部可能低於xml中lay2的頂部。 – hanswim 2014-09-29 06:34:20

+0

@hanswim加號的底部是白色,你不能在這裏看到它,因爲答案編輯器背景也是白色的。試試看我通過測試得到了結果 – XtreemDeveloper 2014-09-29 07:17:51

相關問題