2013-11-14 23 views
32

2 Rölatif düzeni yatay olarak hizalanmış ve ikiye bölünmüş şekilde çizmeye çalıştım.Android: 2 göreceli düzen yarım ekranda bölünmüş

enter image description here

Ben biraz daha iyi ne demek istediğimi açıklamak için boya ile görüntü tasarlamak.

Herhangi bir öneriniz var mı?

+1

kullanabilirsiniz bir [Koru Düzeni] (https://stackoverflow.com/a/46296011/3681880) artık bu tür bir şey yapmak. – Suragch

cevap

40

bir ağırlık vermek bir LinearLayout (yatay yönlendirme), let hepsini içine koymak kastediyorsan Yatay yönlendirmeyle bir LinearLayout içinde, daha sonra RelativeLayouts için weight kullanın. Bu LinearLayout'u 2 eşit parçaya böler.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal" 
    android:baselineAligned="false"> 
    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1"> 
    </RelativeLayout> 
</LinearLayout> 
+0

çalışıyor! Teşekkürler!! – iGio90

+4

Bu çok verimsiz bir düzen olduğunu –

+1

@ V.Kalyuzhnyu tarafından her ne olursa olsun, daha iyi bir sürümü yayınlamak, ben ışıktan daha hızlı ikna edilecektir eminim ... – 2Dee

0

Çiziminizde 4 adet göreli gösterim görüyorum ...?

ortada iki match_parent bir genişliğe sahiptir ve göreli Layouts hem Bu 2 RelativeLayouts koyabilirsiniz 1

+1

Eğer eşit olmayan bir şekilde bölmek istiyorsak o zaman? 0,7 ve 0,3 çalışır? – zIronManBox

94

bir LinearLayout kullanmaya gerek olmadan aynı görevi yerine getirmek için başka bir yol, bir hizalanmış üst düzeni ortasında "takoz", daha sonra buna diğer elemanları hizalama koymaktır. Yarım genişlikteki öğenin genişliğini match_parent olarak ayarlarsanız, ancak hem sol hem de sağ taraflarını hizalarsanız, kendilerini uyacak şekilde daraltırlar.

<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="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.EqualWidthExample" > 

    <!-- An invisible view aligned to the center of the parent. Allows other 
    views to be arranged on either side --> 
    <View 
     android:id="@+id/centerShim" 
     android:layout_height="match_parent" 
     android:layout_width="0dp" 
     android:visibility="invisible" 
     android:layout_centerHorizontal="true"/> 

    <!--Set width to match_parent sets maximum width. alignParentLeft aligns 
    the left edge of this view with the left edge of its parent. toLeftOf 
    sets the right edge of this view to align with the left edge of the 
    given view. The result of all three settings is that this view will 
    always take up exactly half of the width of its parent, however wide 
    that may be. --> 
    <Button 
     android:id="@+id/btLeft" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_toLeftOf="@+id/centerShim" 
     android:text="Left Button" /> 

    <!--Same deal, but on the right --> 
    <Button 
     android:id="@+id/btRight" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_toRightOf="@+id/centerShim" 
     android:layout_below="@+id/tvLeft" 
     android:text="Right Button" /> 
</RelativeLayout> 
+4

'centerShim' görünümünde 'android: layout_height = "0dp" 'yüksekliğini ayarlamak daha iyidir. –

+4

+5 bu yoldan görünüm hiyerarşisi için LinearLayout + layout_weight'den daha verimli –

+2

Brillant! Bir KISS (basit ve aptalca tutun) hilesi! – HoodVinci

4

şimdi kolayca

gradle bağımlılık

dependencies { 
    compile 'com.android.support:percent:25.3.1' 
} 

code in github

012 Güncelleme eklemeyi unutmayın PercentRelativeLayout

<android.support.percent.PercentRelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:padding="16dp" 
tools:context=".MainActivity"> 


<Button 
    android:id="@+id/button" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:text="Button" 
    app:layout_widthPercent="50%"/> 

<Button 
    android:id="@+id/button2" 
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true" 
    android:layout_toRightOf="@id/button" 
    android:text="Button 2" 
    app:layout_widthPercent="50%"/> 
</android.support.percent.PercentRelativeLayout> 
kullanarak bunu yapabilirsiniz

PercentRelativeLayout is Deprecated since API level 26.0.0