代码之家  ›  专栏  ›  技术社区  ›  Edwin ZAP

多布局:匹配父布局、调整内容、调整大小

  •  1
  • Edwin ZAP  · 技术社区  · 6 年前

    我想这样做,但我无法得到我想要的。。。 enter image description here

    这是我的代码:

    <LinearLayout style="@style/currently_playing_box">
        <TextView
            android:text="@string/currently_playing"
            style="@style/currently_playing" />
        <android.support.constraint.ConstraintLayout style="@style/currently_playing_infos">
            <TextView
                android:id="@+id/current_artist"
                android:text="Test"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintEnd_toStartOf="@id/current_radio"
                style="@style/radio_artist" />
            <TextView
                android:text="Test"
                android:id="@+id/current_title"
                app:layout_constraintTop_toBottomOf="@id/current_artist"
                app:layout_constraintEnd_toStartOf="@id/current_radio"
                style="@style/radio_title" />
            <ImageView
                android:id="@+id/current_radio"
                android:layout_width="wrap_content"
                android:layout_height="80dp"
                android:adjustViewBounds="true"
                app:layout_constraintEnd_toEndOf="parent"
                android:src="@drawable/radio_button"/>
        </android.support.constraint.ConstraintLayout>
    </LinearLayout>
    

    我不能让所有条件同时工作

    2 回复  |  直到 6 年前
        1
  •  0
  •   Cheticamp    6 年前

    您正在混合 LinearLayout 用一个 ConstraintLayout . 虽然这是允许的,但您可以使用 ConstraintLayout约束 这将简化您的布局。这是一个模型:

    enter image description here

    我添加了背景色以显示小部件的范围。您似乎也从样式中获得了宽度和高度属性;我已经把它们说清楚了。

    我利用了 match_constraints ( 0dp 在布局中)这是 ConstraintLayout约束 . Here 是关于小部件约束的官方文档,有助于解释发生了什么。

    以下是上图的XML:

    <android.support.constraint.ConstraintLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/holo_red_light"
            android:text="Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing Currently playing "
            tools:ignore="HardcodedText" />
    
        <TextView
            android:id="@+id/current_artist"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/holo_blue_light"
            android:text="Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist Current artist "
            app:layout_constraintEnd_toStartOf="@+id/current_radio"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/title"
            tools:ignore="HardcodedText" />
    
        <TextView
            android:id="@+id/current_title"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="16dp"
            android:layout_marginTop="16dp"
            android:background="@android:color/holo_green_light"
            android:text="Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title Current title "
            app:layout_constraintEnd_toStartOf="@+id/current_radio"
            app:layout_constraintStart_toStartOf="@+id/current_artist"
            app:layout_constraintTop_toBottomOf="@+id/current_artist"
            tools:ignore="HardcodedText" />
    
        <ImageView
            android:id="@+id/current_radio"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            android:layout_marginEnd="16dp"
            android:background="@android:color/holo_orange_dark"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher_foreground"
            app:layout_constraintBottom_toBottomOf="@+id/current_title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="@+id/current_artist"
            tools:ignore="ContentDescription" />
    </android.support.constraint.ConstraintLayout>
    
        2
  •  0
  •   Chetan Kumar Patel    6 年前
        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:layout_marginTop="10dp"
                android:paddingLeft="15dp"
                android:text="Text ONE"
                android:textSize="16sp"
                android:textStyle="bold"/>
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:weightSum="1">
    
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:layout_weight="0.6"
                    android:gravity="center"
                    android:orientation="vertical"
                    android:padding="10dp">
    
                    <TextView
                        android:id="@+id/current_artist"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:layout_marginTop="5dp"
                        android:paddingLeft="15dp"
                        android:text="Text TWO"
                        android:textSize="16sp"
                        android:textStyle="bold"/>
    
                    <TextView
                        android:id="@+id/current_title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="5dp"
                        android:layout_marginTop="5dp"
                        android:paddingLeft="15dp"
                        android:text="Text THREE"
                        android:textSize="16sp"
                        android:textStyle="bold"/>
    
                </LinearLayout>
    
                <ImageView
                    android:id="@+id/current_radio"
                    android:layout_width="wrap_content"
                    android:layout_height="80dp"
                    android:layout_weight="0.4"
                    android:adjustViewBounds="true"
                    android:src="@drawable/mprofilelogo"/>
            </LinearLayout>
    </LinearLayout>
    

    enter image description here

    请检查此XML代码。

    希望这能起作用。。。