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

相关问题