2016-12-07 71 views
-3

如何在RelativeLayout中並排排列兩個視圖? (非線性佈局)Android Studio。 XML佈局寬度在此相關佈局中被壓縮。

我想要在屏幕右側對齊50dp寬度的ViewPager, 並且粉紅色的RelativeLayout佔據了剩餘的空間。

我希望避免在佈局中使用「重量」。

任何解決方案,請?

enter image description here

<?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="120dp"> 

    <RelativeLayout 
     android:id="@+id/dashboard_platform_view" 
     android:layout_width="match_parent" 
     android:background="@color/colorAccent" 
     android:layout_height="match_parent" 
     android:layout_alignParentTop="true" 
     android:orientation="vertical"> 
    </RelativeLayout> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/dashboard_platform_viewpager" 
     android:layout_toRightOf="@id/dashboard_platform_view" 
     android:layout_width="50dp" 
     android:layout_height="match_parent" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:background="@color/colorPrimary" 
     android:minWidth="50dp"> 
    </android.support.v4.view.ViewPager> 

</RelativeLayout> 

回答

1

我檢查了它。所以,在這裏你去:

<?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="120dp"> 

<RelativeLayout 
    android:id="@+id/dashboard_platform_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_alignParentTop="true" 
    android:layout_toStartOf="@id/dashboard_platform_viewpager" 
    android:background="@color/colorAccent" 
    android:orientation="vertical"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/dashboard_platform_viewpager" 
    android:layout_width="50dp" 
    android:layout_height="match_parent" 
    android:layout_alignParentEnd="true" 
    android:background="@color/colorPrimary" 
    android:minWidth="50dp"/> 

</RelativeLayout> 

screenshot

+0

太棒了!謝謝! –

0

嘗試添加android:layout_toLeftOf="@+id/dashboard_platform_viewpager"RelativeLayout的,並刪除toRightOfViewPager

+0

謝謝,但沒有奏效。同樣的事情發生。 「dashboard_platform_viewpager」的寬度變爲0 –

+0

對於dashboard_platform_view,no:android:layout_toLeftOf =「@ + id/dashboard_platform_viewpager」生效,併爲dashboard_platform_viewpager刪除android:layout_toRightOf =「@ id/dashboard_platform_view」 – Kingyal

1

更改您的佈局波紋管,可以幫助您解決問題

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


      <android.support.v4.view.ViewPager 
       android:id="@+id/dashboard_platform_viewpager" 
       android:layout_width="50dp" 
       android:layout_height="match_parent" 
       android:layout_alignParentEnd="true" 
       android:layout_alignParentRight="true" 
       android:background="@color/colorPrimary" 
       android:minWidth="50dp" /> 

      <RelativeLayout 
       android:id="@+id/dashboard_platform_view" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_alignParentTop="true" 
       android:layout_toLeftOf="@id/dashboard_platform_viewpager" 
       android:layout_toStartOf="@id/dashboard_platform_viewpager" 
       android:background="@color/colorAccent" 
       android:orientation="vertical" /> 

     </RelativeLayout> 
+0

添加說明你有什麼變化。 – Ironman