0

我已經反彈了很多,並且一直未能找到與我正在嘗試做的事情相匹配的解決方案。
我有一個LinearLayout中的2個RelativeLayouts。
我想第一個RelativeLayout佔用屏幕的60%。
我想第二個佈局佔據屏幕的其餘部分。佈局,一個ViewGroup佔用了60%的高度,另一個佔據了其餘的高度

我正在使用權重,但我試圖看看是否有一種方法可以避免在第二個RelativeLayout上使用次要權重。
原因是用戶有能力通過按鈕單擊來隱藏第一個佈局。

在代碼中,我通過將第一個RelativeLayout的可見性設置爲「Gone」來完成此操作。
問題是如果我的體重設置爲.6和.4以及總和爲1,當第一個設置爲消失,第二個向上移動正確,但其高度不會增加,因爲它被設置爲40 %。

理想情況下,我可以將它設置爲填充屏幕上的剩餘空間,因此當第一個隱藏時,它將佔用屏幕的全部100%。
這是可能的還是我需要做更多的編程方式當我隱藏第一個視圖增加第二個視圖的允許高度?
希望這是有道理的,如果不讓我知道,我會嘗試進一步澄清。

爲了簡單起見,我已經刪除了RelativeLayouts的一些內容以及一些ID。下面

代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:breadCrumbNavigationLayout="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@color/white" 
android:id="@+id/linearlayout1" 
android:focusable="true" 
android:focusableInTouchMode="true" 
android:weightSum="1"> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:id="@+id/relativeLayout1" 
    android:layout_weight="0.6"> 
    <FrameLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/frame1" /> 
    <ProgressBar 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:id="@+id/progress" 
     android:indeterminateOnly="true" 
     android:keepScreenOn="true" 
     android:visibility="gone" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/relative2"> 
     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="" 
      android:textSize="16dp" 
      android:gravity="center_horizontal" 
      android:layout_below="@+id/relativeLayout1" /> 
     <ListView 
      android:id="@+id/list" 
      android:minWidth="25px" 
      android:minHeight="25px" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:dividerHeight="1dp" /> 
    </RelativeLayout> 
</LinearLayout> 

回答

0

您可以使用Android支持庫的百分比相對佈局來實現這一目標。通過添加下面一行到你的build.gradle(應用級) Percentage Relative Layout

進口比例支持庫,

compile 'com.android.support:percent:24.2.1' 

和你的XML文件將是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:breadCrumbNavigationLayout="http://schemas.android.com/apk/res-auto" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/white" 
    android:id="@+id/linearlayout1" 
    android:focusable="true" 
    android:focusableInTouchMode="true" 
    android:weightSum="1"> 

    <android.support.percent.PercentRelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout 
android:layout_height="0dp" 
android:layout_width="match_parent" 
app:layout_heightPercent="60%" 
android:id="@+id/relativeLayout1" 
> 
<FrameLayout 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/frame1" /> 
<ProgressBar 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:id="@+id/progress" 
    android:indeterminateOnly="true" 
    android:keepScreenOn="true" 
    android:visibility="gone" /> 
</RelativeLayout> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_below="@+id/relativeLayout1" 
    android:id="@+id/relative2"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="" 
     android:textSize="16dp" 
     android:gravity="center_horizontal" 
     android:layout_below="@+id/relativeLayout1" /> 
    <ListView 
     android:id="@+id/list" 
     android:minWidth="25px" 
     android:minHeight="25px" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:dividerHeight="1dp" /> 
</RelativeLayout> 
</android.support.percent.PercentRelativeLayout> 

</LinearLayout> 

一旦您設置,

relativeLayout1.setVisibility(View.GONE); 

你能夠看到相對佈局2將佔據整個父空間。

我希望它能幫助你。祝一切順利。

+0

您可以將第一佈局高度設置爲60%,將第二佈局設置爲低於第一佈局,將第二佈局高度設置爲match_parent。當你隱藏第一個佈局時,它將佔據整個佈局。 –

+1

@Mike M.好的。我會編輯我的答案。謝謝 –

+0

我可以看看這個,但它看起來仍然想要多個百分比來關閉屏幕?是否有可能只設置其中一個內容的百分比,並讓其他內容填充其中? – Kyle

相關問題