多种布局:匹配父级,适合内容,调整大小

时间:2018-02-28 09:52:10

标签: android size android-constraintlayout

我想这样做,但我无法得到我想要的东西...... 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 个答案:

答案 0 :(得分:0)

    <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代码。

希望这会有用......

答案 1 :(得分:0)

您正在混合LinearLayoutConstraintLayout。虽然这是允许的,但您可以使用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>