【问题标题】:Change Constraintlayout to LinearLayout将约束布局更改为线性布局
【发布时间】:2020-05-25 18:06:47
【问题描述】:

:)

我想将我的constraintlayout 更改为LinearLayout。我用绿色(第二个区域)试了一下,效果很好。不幸的是,顶部的效果不如底部。问题是红色区域总是占满整个区域,不去工具栏。 如何使红色区域的高度仅达到工具栏+距离? 我不想在我的工具栏 XML 中更改“更多”内容,否则所有其他页面上的布局都是错误的。 有没有允许android的选项:layout_height = "match_parent- toolbar"

我希望它最终看起来像第二张照片。红色区域应与工具栏保持一定距离并且最大。所以无论红色区域有多少内容,它应该总是尽可能大。我怎样才能做到这一点?我真的很期待有用的答案!提前致谢。

我现在的状态:

我想要达到的目标:

我的尝试

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ececec"
    android:orientation="horizontal">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:elevation="8dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:gravity="center"
        app:cardBackgroundColor="@color/Red"
        app:cardCornerRadius="15dp">

    </androidx.cardview.widget.CardView>

</LinearLayout>

XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:background="#ececec">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewMiddle"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_gravity="center"
        android:layout_marginBottom="8dp"
        android:elevation="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:gravity="center"
        app:cardBackgroundColor="@color/Red"
        app:cardCornerRadius="15dp"
        app:layout_constraintBottom_toTopOf="@+id/cardViewCheckout"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.cardview.widget.CardView>

    <androidx.cardview.widget.CardView
        android:id="@+id/cardViewCheckout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_alignParentStart="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="24dp"
        android:layout_marginEnd="24dp"
        android:layout_marginBottom="10dp"
        android:elevation="8dp"
        app:cardBackgroundColor="#fff"
        app:cardCornerRadius="15dp">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_gravity="center">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    android:layout_marginTop="5sp"
                    android:layout_marginBottom="5sp"
                    android:layout_gravity="center">

                    <TextView
                        android:id="@+id/textViewSumme"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Summe "
                        android:layout_gravity="center"/>
                    <TextView
                        android:id="@+id/textViewSumme2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="0,00"
                        android:layout_gravity="center"/>

                </LinearLayout>

                <Button
                    android:id="@+id/btn_user_geldaufladen"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:layout_marginRight="10dp"
                    android:background="@drawable/button_order_checkout"
                    android:backgroundTint="#04C3B1"
                    android:elevation="16dp"
                    android:text="AUFLADEN"
                    android:textColor="#FFFFFF"
                    android:textStyle="bold"
                    android:layout_marginBottom="10sp"/>

            </LinearLayout>



    </androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>

工具栏

<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
    tools:context=".UserHomeActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/user_content_main"


        />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

【问题讨论】:

    标签: java android xml android-studio android-layout


    【解决方案1】:

    试试这个。

       <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#ececec"
            android:padding="16dp"
            android:orientation="vertical"
            android:layout_below="@+id/appBar"
            android:weightSum="10">
    
            <androidx.cardview.widget.CardView
                android:id="@+id/cardViewMiddle"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="7"
                android:layout_gravity="center"
                android:layout_marginBottom="8dp"
                android:elevation="8dp"
                android:layout_marginEnd="24dp"
                android:layout_marginStart="24dp"
                android:gravity="center"
                app:cardBackgroundColor="#f00"
                app:cardCornerRadius="15dp"
                app:layout_constraintBottom_toTopOf="@+id/cardViewCheckout"
                app:layout_constraintTop_toTopOf="parent">
    
            </androidx.cardview.widget.CardView>
    
            <androidx.cardview.widget.CardView
                android:id="@+id/cardViewCheckout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="3"
                android:layout_marginStart="24dp"
                android:layout_marginEnd="24dp"
                android:layout_marginBottom="10dp"
                android:elevation="8dp"
                app:cardBackgroundColor="#fff"
                app:cardCornerRadius="15dp">
    
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:layout_gravity="center">
    
                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:layout_marginTop="5sp"
                        android:layout_marginBottom="5sp"
                        android:layout_gravity="center">
    
                        <TextView
                            android:id="@+id/textViewSumme"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="Summe "
                            android:layout_gravity="center"/>
                        <TextView
                            android:id="@+id/textViewSumme2"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="0,00"
                            android:layout_gravity="center"/>
    
                    </LinearLayout>
    
                    <Button
                        android:id="@+id/btn_user_geldaufladen"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="10dp"
                        android:background="@color/colorPrimaryDark"
                        android:backgroundTint="#04C3B1"
                        android:elevation="16dp"
                        android:text="AUFLADEN"
                        android:textColor="#FFFFFF"
                        android:textStyle="bold"
                        android:layout_marginBottom="10sp"/>
    
                </LinearLayout>
    
    
    
            </androidx.cardview.widget.CardView>
    
    
        </LinearLayout>
        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/appBar"
            >
    
            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="?attr/colorPrimary"
                />
    
        </com.google.android.material.appbar.AppBarLayout>
    </RelativeLayout>
    

    【讨论】:

      【解决方案2】:

      你可以尝试用这个替换你的(包含)标签吗:

      <include layout="@layout/user_content_main"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
      app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior"
          />
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        • 2020-06-28
        • 2020-01-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多