【问题标题】:Percentage(Weighted) heights for child LinearLayouts inside a parent RelativeLayout父 RelativeLayout 内子 LinearLayouts 的百分比(加权)高度
【发布时间】:2020-02-05 03:13:08
【问题描述】:

我在一个相对布局中直接有两个线性布局。
我希望第一个 LinearLayout 占据 75% 的高度,接下来是 25%。我如何做到这一点?

例如

<RelativeLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"       
        android:weightSum="100"
        android:id="@+id/contentMainLayout">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="75"
            android:id="@+id/linearLayout1">     
            <ScrollView
                android:id="@+id/mainScrollView"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_margin="10dp"    
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <LinearLayout
                    android:id="@+id/mainScrollLL"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">                    
                </LinearLayout>
            </ScrollView>       
        </LinearLayout>
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_weight="25"
            android:layout_marginBottom="20dp" 
            android:layout_below="@+id/mainScrollParentLL"
            android:orientation="horizontal">

            <Button
                android:id="@+id/startTrade"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Stop Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>

</RelativeLayout>

我希望 linearLayout1 使用 75% 的可用高度,而 linearLayout2 使用 25%。
没有为LinearLayout定义layout_weight,所以显然它不起作用。

有什么方法可以实现吗?

这似乎是一个很常见的场景,所以我几乎可以肯定这个问题之前已经被问过。
但我似乎没有找到它。 如果这是重复的问题,请指出我原来的问题,我会关闭它。

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:

    您可以尝试以下代码 - 您需要将 RelativeLayout 更改为 LinearLayout

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/contentMainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:weightSum="100">
    
        <LinearLayout
    
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="75">
    
            <ScrollView
                android:id="@+id/mainScrollView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
    
                <LinearLayout
                    android:id="@+id/mainScrollLL"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"></LinearLayout>
            </ScrollView>
        </LinearLayout>
    
        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_weight="25"
            android:orientation="horizontal">
    
            <Button
                android:id="@+id/startTrade"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Start Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:text="Stop Trade"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      weightSum 不是相对布局的属性。您必须使用线性布局作为父布局,然后才能实现目标。

      参考 - I had similar problem solved using this solution

      【讨论】:

        【解决方案3】:

        请试试这个...

        <LinearLayout
            android:id="@+id/contentMainLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:weightSum="100">
        
            <LinearLayout
                android:id="@+id/linearLayout1"
                android:background="@color/colorAccent"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="75">
        
                <ScrollView
                    android:id="@+id/mainScrollView"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_margin="10dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">
        
                    <LinearLayout
                        android:id="@+id/mainScrollLL"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"/>
                </ScrollView>
            </LinearLayout>
        
            <LinearLayout
                android:id="@+id/linearLayout2"
                android:background="@color/colorPrimary"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="25"
                android:orientation="horizontal">
        
                <Button
                    android:id="@+id/startTrade"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Start Trade"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
        
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:text="Stop Trade"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintLeft_toLeftOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
            </LinearLayout>
        
        </LinearLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-05-09
          • 2014-03-20
          • 1970-01-01
          • 2013-10-12
          • 2015-08-12
          • 2015-09-05
          • 2011-04-25
          • 2021-03-13
          相关资源
          最近更新 更多